Standard library / compress/flate
compress/flate.Writer.Write()
Feed uncompressed bytes into the FLATE compressor. The returned count refers to input consumed, not compressed bytes emitted; buffering may defer writes. Expected stdout: '2 Go\n'.
Formal Syntax & Structure
go syntaxgo
func (w *Writer) Write(data []byte) (n int, err error)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| data | []byte | Uncompressed input bytes. |
Return Value
(n int, err error)
Number of bytes processed and error; check both.
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 w, e := flate.NewWriter(&b, flate.DefaultCompression) must(e) n, e := w.Write([]byte("Go")) must(e) must(w.Close()) fmt.Println(n, unpack(b.Bytes()))}