How can I use gzip on a string?
You can just write using gzip.Writer
as it implements io.Writer
.
Example:
package main
import (
"bytes"
"compress/gzip"
"fmt"
"log"
)
func main() {
var b bytes.Buffer
gz := gzip.NewWriter(&b)
if _, err := gz.Write([]byte("YourDataHere")); err != nil {
log.Fatal(err)
}
if err := gz.Close(); err != nil {
log.Fatal(err)
}
fmt.Println(b.Bytes())
}
Go Playground
If you want to set the compression level (Default is -1 from compress/flate
) you can use gzip.NewWriterLevel
.