Standard library / compress/lzw
compress/lzw.Writer.Write()
Feed literal bytes into the LZW compressor. The count is uncompressed bytes accepted; a literal outside the configured width is invalid. Expected stdout: '2 Go\n'.
Formal Syntax & Structure
go syntaxgo
func (w *Writer) Write(p []byte) (n int, err error)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| p | []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/lzw" "fmt" "io")
func must(e error) { if e != nil { panic(e) }}func pack(s string, order lzw.Order) []byte { var b bytes.Buffer w := lzw.NewWriter(&b, order, 8) _, e := w.Write([]byte(s)) must(e) must(w.Close()) return b.Bytes()}func unpack(b []byte, order lzw.Order) string { r := lzw.NewReader(bytes.NewReader(b), order, 8) p, e := io.ReadAll(r) must(e) must(r.Close()) return string(p)}func main() { var b bytes.Buffer w := lzw.NewWriter(&b, lzw.LSB, 8) n, e := w.Write([]byte("Go")) must(e) must(w.Close()) fmt.Println(n, unpack(b.Bytes(), lzw.LSB))}