Source file src/compress/lzw/example_test.go

     1  // Copyright 2026 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package lzw_test
     6  
     7  import (
     8  	"bytes"
     9  	"compress/lzw"
    10  	"fmt"
    11  	"io"
    12  	"log"
    13  )
    14  
    15  func Example() {
    16  	const original = "Hello, Gophers!"
    17  
    18  	var compressed bytes.Buffer
    19  	// LSB and an 8-bit literal width are the settings used by GIF.
    20  	w := lzw.NewWriter(&compressed, lzw.LSB, 8)
    21  	if _, err := w.Write([]byte(original)); err != nil {
    22  		log.Fatal(err)
    23  	}
    24  	if err := w.Close(); err != nil {
    25  		log.Fatal(err)
    26  	}
    27  	r := lzw.NewReader(&compressed, lzw.LSB, 8)
    28  	decoded, err := io.ReadAll(r)
    29  	if err != nil {
    30  		log.Fatal(err)
    31  	}
    32  	if err := r.Close(); err != nil {
    33  		log.Fatal(err)
    34  	}
    35  
    36  	fmt.Println(string(decoded))
    37  	// Output: Hello, Gophers!
    38  }
    39  

View as plain text