...

Source file src/compress/zlib/example_test.go

Documentation: compress/zlib

     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 0 13 0 242 255 104 101 108 108 111 44 32 119 111 114 108 100 10 3 0 33 231 4 147]
    23  }
    24  
    25  func ExampleNewReader() {
    26  	buff := []byte{120, 156, 0, 13, 0, 242, 255, 104, 101, 108, 108, 111,
    27  		44, 32, 119, 111, 114, 108, 100, 10, 3, 0, 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  	r.Close()
    36  	// Output:
    37  	// hello, world
    38  }
    39  

View as plain text