Standard library / compress/lzw

compress/lzw.Reader.Reset()

Reuse the decoder for a new source, order and literal width. Old buffered state is discarded. Expected stdout: 'Go\n'.

Formal Syntax & Structure

go syntaxgo
func (r *Reader) Reset(src io.Reader, order Order, litWidth int)

Parameters & Attributes

NameTypeDescription
srcio.ReaderNew compressed input stream.
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 r lzw.Reader  r.Reset(bytes.NewReader(pack("old", lzw.LSB)), lzw.LSB, 8)  r.Reset(bytes.NewReader(pack("Go", lzw.MSB)), lzw.MSB, 8)  p, e := io.ReadAll(&r)  must(e)  must(r.Close())  fmt.Println(string(p))}
Go Reference