[code=golang]
import (
"bytes"
"compress/gzip"
)
func gzencode(str string) ([]byte, error) {
// Create a bytes.Buffer to store the compressed data
var buf bytes.Buffer
// Create a gzip.Writer to write the compressed data to buf
gz := gzip.NewWriter(&buf)
// Write the original string to the gzip.Writer
_, err := gz.Write([]byte(str))
if err != nil {
return nil, err
}
// Close the gzip.Writer to flush the buffer to buf
err = gz.Close()
if err != nil {
return nil, err
}
// Return the compressed data
return buf.Bytes(), nil
}
[/code]
|