Standard library / compress/flate

compress/flate.NewReader()

Construct a raw DEFLATE decompression reader. Unlike ZLIB and GZIP, the format does not add their framing or checksum. Errors appear on Read. Expected stdout: 'Go\n'.

Formal Syntax & Structure

go syntaxgo
func NewReader(r io.Reader) io.ReadCloser

Parameters & Attributes

NameTypeDescription
rio.ReaderCompressed input stream. Implement io.ByteReader to avoid unnecessary read-ahead where applicable.

Return Value

io.ReadCloser

Decompression reader; read to EOF to validate the stream where applicable, then close.

Working Example

Example Snippetgo
package main
import (  "bytes"  "compress/flate"  "fmt"  "io")
func must(e error) {  if e != nil {    panic(e)  }}func pack(s string) []byte {  var b bytes.Buffer  w, e := flate.NewWriter(&b, flate.DefaultCompression)  must(e)  _, e = w.Write([]byte(s))  must(e)  must(w.Close())  return b.Bytes()}func unpack(b []byte) string {  r := flate.NewReader(bytes.NewReader(b))  p, e := io.ReadAll(r)  must(e)  must(r.Close())  return string(p)}func main() {  r := flate.NewReader(bytes.NewReader(pack("Go")))  p, e := io.ReadAll(r)  must(e)  must(r.Close())  fmt.Println(string(p))}
Go Reference