title: How to convert (type *bytes.Buffer) to use as []byte in argument to w.Write
body: <p>I'm trying to return some json back from the server but get this error with the following code</p>
  
  <pre><code>cannot use buffer (type *bytes.Buffer) as type []byte in argument to w.Write
  </code></pre>
  
  <p>With a little googling, I found <a href="https://stackoverflow.com/questions/23454940/getting-bytes-buffer-does-not-implement-io-writer-error-message">this SO answer</a> but couldn't get it to work (see second code sample with error message)</p>
  
  <p>1st code sample</p>
  
  <pre><code>buffer := new(bytes.Buffer)
  
  for _, jsonRawMessage := range sliceOfJsonRawMessages{
      if err := json.Compact(buffer, jsonRawMessage); err != nil{
          fmt.Println("error")
  
      }
  
  }   
  fmt.Println("json returned", buffer)//this is json
  w.Header().Set("Content-Type", contentTypeJSON)
  
  w.Write(buffer)//error: cannot use buffer (type *bytes.Buffer) as type []byte in argument to w.Write
  </code></pre>
  
  <p>2nd code sample with error</p>
  
  <pre><code>cannot use foo (type *bufio.Writer) as type *bytes.Buffer in argument to json.Compact
   cannot use foo (type *bufio.Writer) as type []byte in argument to w.Write
  
  
  var b bytes.Buffer
  foo := bufio.NewWriter(&amp;b)
  
  for _, d := range t.J{
      if err := json.Compact(foo, d); err != nil{
          fmt.Println("error")
  
      }
  
  }
  
  
  w.Header().Set("Content-Type", contentTypeJSON)
  
  w.Write(foo)
  </code></pre>