title: gzip compression to http responseWriter
body: <p>I'm new to Go. But am playing with a REST Api. I cant get the same behavior out of json.Marshal as json.Encoder in two functions i have</p>
  
  <p>I wanted to use this function to gzip my responses:</p>
  
  <pre><code>func gzipFast(a *[]byte) []byte {
      var b bytes.Buffer
      gz := gzip.NewWriter(&amp;b)
      defer gz.Close()
      if _, err := gz.Write(*a); err != nil {
          panic(err)
      }
      return b.Bytes()
  }
  </code></pre>
  
  <p>But this function returns this:</p>
  
  <pre><code>curl http://localhost:8081/compressedget --compressed --verbose
  *   Trying 127.0.0.1...
  * Connected to localhost (127.0.0.1) port 8081 (#0)
  &gt; GET /compressedget HTTP/1.1
  &gt; Host: localhost:8081
  &gt; User-Agent: curl/7.47.0
  &gt; Accept: */*
  &gt; Accept-Encoding: deflate, gzip
  &gt; 
  &lt; HTTP/1.1 200 OK
  &lt; Content-Encoding: gzip
  &lt; Content-Type: application/json
  &lt; Date: Wed, 24 Aug 2016 00:59:38 GMT
  &lt; Content-Length: 30
  &lt; 
  * Connection #0 to host localhost left intact
  </code></pre>
  
  <p>Here is the go function:</p>
  
  <pre><code>func CompressedGet(w http.ResponseWriter, r *http.Request, ps  httprouter.Params) {
  
      box := Box{Width: 10, Height: 20, Color: "gree", Open: false}
      box.ars = make([]int, 100)
      for i := range box.ars {
          box.ars[i] = i
      }
      //fmt.Println(r.Header.Get("Content-Encoding"))
  
      w.Header().Set("Content-Type", "application/json")
      w.Header().Set("Content-Encoding", "gzip")
      b, _ := json.Marshal(box)
      //fmt.Println(len(b))
      //fmt.Println(len(gzipFast(&amp;b)))
  
      fmt.Fprint(w, gzipFast(&amp;b))
      //fmt.Println(len(gzipSlow(b)))
      //gz := gzip.NewWriter(w)
      //defer gz.Close()
      //json.NewEncoder(gz).Encode(box)
      r.Body.Close()
  
  }
  </code></pre>
  
  <p>But when i uncomment:</p>
  
  <pre><code>//gz := gzip.NewWriter(w)
  //defer gz.Close()
  //json.NewEncoder(gz).Encode(box)
  </code></pre>
  
  <p>it works fine.</p>