Note that there are some explanatory texts on larger screens.

plurals
  1. POReading a local file in Google App Engine with Go
    primarykey
    data
    text
    <p>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.</p> <pre><code>panic: runtime error: invalid memory address or nil pointer dereference </code></pre> <p>I'm pretty confused, however it will run without error if I comment out</p> <pre><code>channel &lt;- buffer[0:dat] </code></pre> <p>So I must be using channels incorrectly, Any help?</p> <p>Edit:</p> <p>This is the working code, many thanks to Kevin Ballard for helping me get this one.</p> <pre><code>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 &lt;- 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) } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload