使用 Go 读取 Google App Engine 中的本地文件

发布于 2024-11-28 03:50:00 字数 1714 浏览 3 评论 0原文

我正在尝试在谷歌应用程序引擎上的网站上使用 go 而不是 python 。但是当我在本地测试时,我的脚本不断出现此错误。

panic: runtime error: invalid memory address or nil pointer dereference

我很困惑,但是如果我注释掉它会运行而不会出错

channel <- buffer[0:dat]

所以我一定是错误地使用了通道,有什么帮助吗?

编辑:

这是工作代码,非常感谢 Kevin Ballard 帮助我得到这个代码。

package defp

import (
    "fmt"
    "http"
    "os"
)

func getContent(filename string, channel chan []byte) {
    file, err := os.OpenFile(filename, os.O_RDONLY, 0666)
    defer file.Close()
    if err == nil {
        fmt.Printf("FILE FOUND : " + filename + " \n")
        buffer := make([]byte, 16)
        dat, err := file.Read(buffer)
        for err == nil {
            fmt.Printf("herp")
            channel <- buffer[0:dat]
            buffer = make([]byte, 16)
            dat, err = file.Read(buffer)
        }
        close(channel)
        fmt.Printf("DONE READING\n")
    } else {
        fmt.Printf("FILE NOT FOUND : " + filename + " \n")
    }
}
func writeContent(w http.ResponseWriter, channel chan []byte) {
    fmt.Printf("ATTEMPTING TO WRITE CONTENT\n")
    go func() {
        for bytes := range channel {
            w.Write(bytes)
            fmt.Printf("BYTES RECEIVED\n")
        }
    }()
    fmt.Printf("FINISHED WRITING\n")
}
func load(w http.ResponseWriter, path string) {
    fmt.Printf("ATTEMPTING LOAD " + path + "\n")
    channel := make(chan []byte, 50)
    writeContent(w, channel)
    getContent(path, channel)
}
func handle(w http.ResponseWriter, r *http.Request) {
    fmt.Printf("HANDLING REQUEST FOR " + r.URL.Path[1:] + "\n")
    load(w, r.URL.Path[1:])
}
func init() {
    http.HandleFunc("/", handle)
}

I'm trying to use go instead of python for my website on google app engine. But I keep getting this error with my script when I test locally.

panic: runtime error: invalid memory address or nil pointer dereference

I'm pretty confused, however it will run without error if I comment out

channel <- buffer[0:dat]

So I must be using channels incorrectly, Any help?

Edit:

This is the working code, many thanks to Kevin Ballard for helping me get this one.

package defp

import (
    "fmt"
    "http"
    "os"
)

func getContent(filename string, channel chan []byte) {
    file, err := os.OpenFile(filename, os.O_RDONLY, 0666)
    defer file.Close()
    if err == nil {
        fmt.Printf("FILE FOUND : " + filename + " \n")
        buffer := make([]byte, 16)
        dat, err := file.Read(buffer)
        for err == nil {
            fmt.Printf("herp")
            channel <- buffer[0:dat]
            buffer = make([]byte, 16)
            dat, err = file.Read(buffer)
        }
        close(channel)
        fmt.Printf("DONE READING\n")
    } else {
        fmt.Printf("FILE NOT FOUND : " + filename + " \n")
    }
}
func writeContent(w http.ResponseWriter, channel chan []byte) {
    fmt.Printf("ATTEMPTING TO WRITE CONTENT\n")
    go func() {
        for bytes := range channel {
            w.Write(bytes)
            fmt.Printf("BYTES RECEIVED\n")
        }
    }()
    fmt.Printf("FINISHED WRITING\n")
}
func load(w http.ResponseWriter, path string) {
    fmt.Printf("ATTEMPTING LOAD " + path + "\n")
    channel := make(chan []byte, 50)
    writeContent(w, channel)
    getContent(path, channel)
}
func handle(w http.ResponseWriter, r *http.Request) {
    fmt.Printf("HANDLING REQUEST FOR " + r.URL.Path[1:] + "\n")
    load(w, r.URL.Path[1:])
}
func init() {
    http.HandleFunc("/", handle)
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

明月夜 2024-12-05 03:50:00

您的程序有时会出现恐慌的原因是,它有时会在程序退出 load 函数后写入 w http.ResponseWriter 。当程序退出处理函数时,http 包会自动关闭 http.ResponseWriter。在函数 writeContent 中,程序有时会尝试写入已关闭的 http.ResponseWriter

顺便说一句:如果使用 io.Copy 函数,您可以使程序源代码变得更小。

为了始终获得可预测的行为,请确保在退出处理程序函数之前完成您希望程序响应 HTTP 请求而执行的所有工作。例如:

func writeContent(w http.ResponseWriter, channel chan []byte) {
    fmt.Printf("ATTEMPTING TO WRITE CONTENT\n")
    for bytes := range channel {
            w.Write(bytes)
            fmt.Printf("BYTES RECEIVED\n")
    }
    fmt.Printf("FINISHED WRITING\n")
}

func load(w http.ResponseWriter, path string) {
    fmt.Printf("ATTEMPTING LOAD " + path + "\n")
    channel := make(chan []byte)
    workDone := make(chan byte)
    go func() {
            writeContent(w, channel)
            workDone <- 1 //Send an arbitrary value
    }()
    go func() {
            getContent(path, channel)
            workDone <- 2 //Send an arbitrary value
    }()
    <-workDone
    <-workDone
}

func handle(w http.ResponseWriter, r *http.Request) {
    fmt.Printf("HANDLING REQUEST FOR " + r.URL.Path[1:] + "\n")
    load(w, r.URL.Path[1:])
}

The reason why your program sometimes panics is that it is sometimes writing to w http.ResponseWriter after the program exits the load function. The http package automatically closes the http.ResponseWriter when the program exits the handler function. In function writeContent, the program will sometimes attempt to write to a closed http.ResponseWriter.

BTW: You can make the program source code much smaller if you use the io.Copy function.

To always get predictable behavior, make sure that all work that you want the program to perform in response to a HTTP request is done before you exit the handler function. For example:

func writeContent(w http.ResponseWriter, channel chan []byte) {
    fmt.Printf("ATTEMPTING TO WRITE CONTENT\n")
    for bytes := range channel {
            w.Write(bytes)
            fmt.Printf("BYTES RECEIVED\n")
    }
    fmt.Printf("FINISHED WRITING\n")
}

func load(w http.ResponseWriter, path string) {
    fmt.Printf("ATTEMPTING LOAD " + path + "\n")
    channel := make(chan []byte)
    workDone := make(chan byte)
    go func() {
            writeContent(w, channel)
            workDone <- 1 //Send an arbitrary value
    }()
    go func() {
            getContent(path, channel)
            workDone <- 2 //Send an arbitrary value
    }()
    <-workDone
    <-workDone
}

func handle(w http.ResponseWriter, r *http.Request) {
    fmt.Printf("HANDLING REQUEST FOR " + r.URL.Path[1:] + "\n")
    load(w, r.URL.Path[1:])
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文