golang/go · error

lzw: unknown order

Error message

lzw: unknown order

What it means

Writer.init switches on the Order argument and only accepts lzw.LSB and lzw.MSB. Any other value lands in the default branch, sets w.err, and returns. The Writer appears to construct successfully but every subsequent Write returns the sticky error.

Source

Thrown at src/compress/lzw/writer.go:274

// is a *[Writer].
func NewWriter(w io.Writer, order Order, litWidth int) io.WriteCloser {
	return newWriter(w, order, litWidth)
}

func newWriter(dst io.Writer, order Order, litWidth int) *Writer {
	w := new(Writer)
	w.init(dst, order, litWidth)
	return w
}

func (w *Writer) init(dst io.Writer, order Order, litWidth int) {
	switch order {
	case LSB:
		w.write = (*Writer).writeLSB
	case MSB:
		w.write = (*Writer).writeMSB
	default:
		w.err = errors.New("lzw: unknown order")
		return
	}
	if litWidth < 2 || 8 < litWidth {
		w.err = fmt.Errorf("lzw: litWidth %d out of range", litWidth)
		return
	}
	bw, ok := dst.(writer)
	if !ok && dst != nil {
		bw = bufio.NewWriter(dst)
	}
	w.w = bw
	lw := uint(litWidth)
	w.order = order
	w.width = 1 + lw
	w.litWidth = lw
	w.hi = 1<<lw + 1
	w.overflow = 1 << (lw + 1)
	w.savedCode = invalidCode

View on GitHub (pinned to b6b368adc5)

Solutions

  1. Pass one of the package constants: lzw.LSB for GIF, lzw.MSB for PDF.
  2. Always check the error returned by the first Write call (or call Flush) — NewWriter itself never returns an error.
  3. Add a guard in your wrapper that rejects Order values outside {LSB, MSB} before constructing the Writer.

Example fix

// before
w := lzw.NewWriter(dst, lzw.Order(2), 8) // invalid
n, err := w.Write(data) // err = "lzw: unknown order"

// after
w := lzw.NewWriter(dst, lzw.LSB, 8)
n, err := w.Write(data)
Defensive patterns

Strategy: validation

Validate before calling

func validOrder(o lzw.Order) error {
    switch o {
    case lzw.LSB, lzw.MSB: return nil
    default: return fmt.Errorf("invalid lzw.Order %d", o)
    }
}

func newLZWWriter(dst io.Writer, order lzw.Order, litWidth int) (*lzw.Writer, error) {
    if err := validOrder(order); err != nil { return nil, err }
    return lzw.NewWriter(dst, order, litWidth), nil
}

Type guard

func isValidOrder(o lzw.Order) bool {
    return o == lzw.LSB || o == lzw.MSB
}

Try / catch

// lzw.NewWriter never returns an error; check the first Write.
if _, err := w.Write(data); err != nil {
    if err.Error() == "lzw: unknown order" { /* fix the Order argument */ }
}

Prevention

When it happens

Trigger: Calling lzw.NewWriter(dst, order, litWidth) where order is neither lzw.LSB nor lzw.MSB. Because the constructor returns a non-nil *Writer, callers that ignore the first Write's error can write nothing silently.

Common situations: Same shape as the reader variant: zero-value Order variable, refactored enum, or unit test using a sentinel int cast to Order.

Related errors


AI-assisted analysis of golang/go@b6b368adc5 (2026-08-12). Data as JSON: /api/errors/03f03fbef867f022. Report an issue: GitHub.