Standard library / compress/lzw

compress/lzw.Writer.Reset()

Reuse the writer with a new destination, bit order and literal width. Finalize the previous stream before resetting if it must remain usable. Expected stdout: 'Go next\n'.

Formal Syntax & Structure

go syntaxgo
func (w *Writer) Reset(dst io.Writer, order Order, litWidth int)

Parameters & Attributes

NameTypeDescription
dstio.WriterNew destination for the reset compressor.
orderOrderBit packing order, LSB or MSB. Match the encoder and decoder.
litWidthintLiteral code width in bits, from 2 through 8.

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, c bytes.Buffer  var w lzw.Writer  w.Reset(&b, lzw.LSB, 8)  _, e := w.Write([]byte("Go"))  must(e)  must(w.Close())  w.Reset(&c, lzw.MSB, 8)  _, e = w.Write([]byte("next"))  must(e)  must(w.Close())  fmt.Println(unpack(b.Bytes(), lzw.LSB), unpack(c.Bytes(), lzw.MSB))}
Go Reference