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)
}
The reason why your program sometimes panics is that it is sometimes writing to
w http.ResponseWriterafter the program exits theloadfunction. Thehttppackage automatically closes thehttp.ResponseWriterwhen 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.Copyfunction.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: