使用 Go 读取 Google App Engine 中的本地文件
我正在尝试在谷歌应用程序引擎上的网站上使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的程序有时会出现恐慌的原因是,它有时会在程序退出
load
函数后写入w http.ResponseWriter
。当程序退出处理函数时,http
包会自动关闭http.ResponseWriter
。在函数writeContent
中,程序有时会尝试写入已关闭的http.ResponseWriter
。顺便说一句:如果使用 io.Copy 函数,您可以使程序源代码变得更小。
为了始终获得可预测的行为,请确保在退出处理程序函数之前完成您希望程序响应 HTTP 请求而执行的所有工作。例如:
The reason why your program sometimes panics is that it is sometimes writing to
w http.ResponseWriter
after the program exits theload
function. Thehttp
package automatically closes thehttp.ResponseWriter
when the program exits the handler function. In functionwriteContent
, the program will sometimes attempt to write to a closedhttp.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: