title: Reading a local file in Google App Engine with Go
body: <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>