Standard library / compress/flate

compress/flate.Writer

Stateful DEFLATE compressor. Use Write and Close to create complete streams and Reset to reuse allocation after completion. Expected stdout: 'Go\n'.

Formal Syntax & Structure

go syntaxgo
type Writer struct {  // Has unexported fields.}

Working Example

Example Snippetgo
package main
import (  "bytes"  "compress/flate"  "fmt"  "io")
func must(e error) {  if e != nil {    panic(e)  }}func pack(s string) []byte {  var b bytes.Buffer  w, e := flate.NewWriter(&b, flate.DefaultCompression)  must(e)  _, e = w.Write([]byte(s))  must(e)  must(w.Close())  return b.Bytes()}func unpack(b []byte) string {  r := flate.NewReader(bytes.NewReader(b))  p, e := io.ReadAll(r)  must(e)  must(r.Close())  return string(p)}func main() {  var b bytes.Buffer  var w *flate.Writer  w, e := flate.NewWriter(&b, flate.DefaultCompression)  must(e)  _, e = w.Write([]byte("Go"))  must(e)  must(w.Close())  fmt.Println(unpack(b.Bytes()))}
Go Reference