example_test.go 784 B

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