How can I use the “compress/gzip” package to gzip a file?

前端 未结 5 1244
长情又很酷
长情又很酷 2020-12-13 18:48

I\'m new to Go, and can\'t figure out how to use the compress/gzip package to my advantage. Basically, I just want to write something to a file, gzip it and rea

5条回答
  •  情深已故
    2020-12-13 19:12

    All the compress packages implement the same interface. You would use something like this to compress:

    var b bytes.Buffer
    w := gzip.NewWriter(&b)
    w.Write([]byte("hello, world\n"))
    w.Close()
    

    And this to unpack:

    r, err := gzip.NewReader(&b)
    io.Copy(os.Stdout, r)
    r.Close()
    

提交回复
热议问题