title: Invalid memory address using gorutine (golang)
body: <p>I am working with a code that works only if I make it run as a blocking routine... but if I use <code>go executeTheFunction</code> I always get <code>panic: runtime error: invalid memory address or nil pointer dereference</code>. To summarize the code a little would be something like that.</p>
  
  <pre><code>func (c App) Index() revel.Result {
      app = c; //this is a "global" variable to be used in callBackFunction 
      options = "foo";
      client  = NewClient()
      client.RequestAsynch(options, callBackFunction);
  }
  func callBackFunction(message string){
      reader := bytes.NewBufferString(message)
      _, err := xmlpath.Parse( reader )
      if err != nil {
          panic(err)
      }
      flusher, ok := airshopping.Response.Out.(http.Flusher)
      if !ok {
        panic("expected http.ResponseWriter to be an http.Flusher")
      }
  
      fmt.Fprintf(airshopping.Response.Out, "whatever")
      flusher.Flush()
  }
  
  func (client * Client)RequestAsynch(message Message, callback func(string)) {
      Response := client.Request(message)
  
      message_aux := ""
      reader := bufio.NewReader(Response.Body)
      for {
          line, err := reader.ReadBytes('\n')
          if err==nil {
              message_aux = message_aux + string(line)
              if strings.Contains(message_aux, "&lt;!-- AG-EOM --&gt;"){
                  go callback(message_aux)
                  message_aux = ""
              }
          }else{fmt.Println("ERROR", err);break;}
      }
  }
  </code></pre>
  
  <p>Ok... I am going to explain a little. The <code>RequestAsynch</code> is in a different package and what it launchs a request to other server. This other server reponse with multiple xml... the body of these xml looks like:</p>
  
  <pre><code>&lt;!-- Header status:ok --&gt;
  &lt;all_the_xml_response&gt;
  &lt;!-- AG-EOM --&gt;
  </code></pre>
  
  <p>So... when the line by line loop detect this <code>"&lt;!-- AG-EOM --&gt;"</code> is that one of the xml is completely received, and the callBack functions should be executed (with a gorutine). The mission of this callBack function is parse the xml and, after a process of parsing, send some data back to the javascript in the client side.</p>
  
  <p>The error I am always getting is this:</p>
  
  <pre><code>panic: runtime error: invalid memory address or nil pointer dereference
  [signal 0xc0000005 code=0x0 addr=0x20 pc=0x4f8c09]
  
  goroutine 80 [running]:
  bufio.(*Writer).Write(0xc042454540, 0xc0422d4000, 0x11076, 0x12000, 0xc04202fb30, 0x4683b7, 0x11076)
      C:/Go/src/bufio/bufio.go:598 +0x149
  net/http.(*response).write(0xc04234d0a0, 0x11076, 0xc0422d4000, 0x11076, 0x12000, 0x0, 0x0, 0xc0422d4000, 0x0, 0x0)
      C:/Go/src/net/http/server.go:1525 +0x157
  net/http.(*response).Write(0xc04234d0a0, 0xc0422d4000, 0x11076, 0x12000, 0x0, 0x0, 0xc042824000)
      C:/Go/src/net/http/server.go:1495 +0x6b
  fmt.Fprintf(0xb65e80, 0xc04234d0a0, 0xc042824000, 0x11076, 0x0, 0x0, 0x0, 0xc0421445f0, 0xb, 0xc0422f5e00)
      C:/Go/src/fmt/print.go:182 +0xb0
  ndc-console-test/app/controllers/ndcmethods.GetOffers_AS(0xc042594000, 0xef7f)
      F:/Proyectos_Trabajo/work/src/ndc-console-    test/app/controllers/ndcmethods/airshopping.go:98 +0x7a1
  created by github.com/open-ndc/ndc-go-sdk.(*Client).RequestAsynch
      F:/Proyectos_Trabajo/work/src/github.com/open-ndc/ndc-go-sdk/ndc_client.go:143 +0x25c
  2017/05/17 16:42:56 reverseproxy.go:316: http: proxy error: dial tcp [::1]:55058: connectex:A connection can not be established because the target computer expressly denied the connection.
  </code></pre>
  
  <p>And after this error... I need to restart the server, because it doesn´t allow make a new connection.  </p>
  
  <p>One of the things I saw, is that if I remove this line <code>_, err := xmlpath.Parse( reader )</code> in the callBackFunction it works fine. And of course if I change this line: <code>go callback(message_aux)</code> for <code>callback(message_aux)</code> (without the goroutine) it works fine too and it has no problem to parse the xml.  </p>
  
  <p>Does anyone know a solution about how to avoid this error using goroutines??</p>
  
  <p>Thank you very much.</p>
  
  <h2>EDIT</h2>
  
  <p>I think I did not express myself well with how the other server responds, this is how looks the (multiple) responses:</p>
  
  <pre><code>&lt;!-- Header status:ok --&gt;
  &lt;the_xml_response_of_one_response_or_one_chunk&gt;
  &lt;!-- AG-EOM --&gt;
  &lt;!-- Header status:ok --&gt;
  &lt;other_xml_response&gt;
  &lt;!-- AG-EOM --&gt;
  &lt;!-- Header status:ok --&gt;
  &lt;other_more_xml_response&gt;
  &lt;!-- AG-EOM --&gt;
  </code></pre>