title: Golang: Convert to JSON.GZ and write to file
body: <p>Trying to accomplish the following output with my data:</p>
  
  <ol>
  <li>Convert to JSON string and write to file: output.json (this part is working)</li>
  <li>Gzip Compress the JSON string and write that to a json.gz file: output.json.gz (<strong>NOT WORKING</strong>)</li>
  </ol>
  
  <p>The code runs fine and writes to both files. But the gzipped file gives this error when I try to unzip it: <code>Data error in 'output.json'. File is broken</code></p>
  
  <p>Here's the code:</p>
  
  <pre><code>package main
  
  import (
      "bytes"
      "compress/gzip"
      "encoding/json"
      "fmt"
      "io/ioutil"
  )
  
  type Generic struct {
      Name string
      Cool bool
      Rank int
  }
  
  func main() {
      generic := Generic{"Golang", true, 100}
      fileJson, _ := json.Marshal(generic)
      err := ioutil.WriteFile("output.json", fileJson, 0644)
      if err != nil {
          fmt.Printf("WriteFileJson ERROR: %+v", err)
      }
  
      var fileGZ bytes.Buffer
      zipper := gzip.NewWriter(&amp;fileGZ)
      defer zipper.Close()
      _, err = zipper.Write([]byte(string(fileJson)))
      if err != nil {
          fmt.Printf("zipper.Write ERROR: %+v", err)
      }
      err = ioutil.WriteFile("output.json.gz", []byte(fileGZ.String()), 0644)
      if err != nil {
          fmt.Printf("WriteFileGZ ERROR: %+v", err)
      }
  }
  </code></pre>
  
  <p>What did I miss?</p>