github.com/liujq9674git/golang-src-1.7@v0.0.0-20230517174348-17f6ec47f3f8/src/compress/zlib/example_test.go (about)

     1  // Copyright 2012 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 zlib_test
     6  
     7  import (
     8  	"bytes"
     9  	"compress/zlib"
    10  	"fmt"
    11  	"io"
    12  	"os"
    13  )
    14  
    15  func ExampleNewWriter() {
    16  	var b bytes.Buffer
    17  
    18  	w := zlib.NewWriter(&b)
    19  	w.Write([]byte("hello, world\n"))
    20  	w.Close()
    21  	fmt.Println(b.Bytes())
    22  	// Output: [120 156 202 72 205 201 201 215 81 40 207 47 202 73 225 2 4 0 0 255 255 33 231 4 147]
    23  }
    24  
    25  func ExampleNewReader() {
    26  	buff := []byte{120, 156, 202, 72, 205, 201, 201, 215, 81, 40, 207,
    27  		47, 202, 73, 225, 2, 4, 0, 0, 255, 255, 33, 231, 4, 147}
    28  	b := bytes.NewReader(buff)
    29  
    30  	r, err := zlib.NewReader(b)
    31  	if err != nil {
    32  		panic(err)
    33  	}
    34  	io.Copy(os.Stdout, r)
    35  	// Output: hello, world
    36  	r.Close()
    37  }