Standard library / compress/lzw

compress/lzw.NewReader()

Construct a decompressor using the supplied bit packing order and literal width (2–8). Invalid settings yield a reader that reports errors. Match the encoder settings. Expected stdout: 'Go\n'.

Formal Syntax & Structure

go syntaxgo
func NewReader(r io.Reader, order Order, litWidth int) io.ReadCloser

Parameters & Attributes

NameTypeDescription
rio.ReaderCompressed input stream. Implement io.ByteReader to avoid unnecessary read-ahead where applicable.
orderOrderBit packing order, LSB or MSB. Match the encoder and decoder.
litWidthintLiteral code width in bits, from 2 through 8.

Return Value

io.ReadCloser

Decompression reader; read to EOF to validate the stream where applicable, then close.

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() {  r := lzw.NewReader(bytes.NewReader(pack("Go", lzw.LSB)), lzw.LSB, 8)  p, e := io.ReadAll(r)  must(e)  must(r.Close())  fmt.Println(string(p))}
Go Reference