Standard library / compress/zlib
compress/zlib.NewReader()
Read a ZLIB header and construct a decompression reader. ZLIB wraps DEFLATE data with header and Adler-32 trailer; read to EOF to validate it. Expected stdout: 'Go\n'.
Formal Syntax & Structure
go syntaxgo
func NewReader(r io.Reader) (io.ReadCloser, error)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| r | io.Reader | Compressed input stream. Implement io.ByteReader to avoid unnecessary read-ahead where applicable. |
Return Value
(io.ReadCloser, error)
New reader or header or dictionary validation error.
Working Example
Example Snippetgo
package main
import ( "bytes" "compress/zlib" "fmt" "io")
func must(e error) { if e != nil { panic(e) }}func pack(s string) []byte { var b bytes.Buffer w := zlib.NewWriter(&b) _, e := w.Write([]byte(s)) must(e) must(w.Close()) return b.Bytes()}func unpack(b []byte) string { r, e := zlib.NewReader(bytes.NewReader(b)) must(e) p, e := io.ReadAll(r) must(e) must(r.Close()) return string(p)}func main() { r, e := zlib.NewReader(bytes.NewReader(pack("Go"))) must(e) p, e := io.ReadAll(r) must(e) must(r.Close()) fmt.Println(string(p))}