Standard library / compress/zlib
compress/zlib.NewReaderDict()
Read ZLIB data with a supplied preset dictionary. A mismatch may be rejected at header parsing with ErrDictionary. Expected stdout: 'Go\n'.
Formal Syntax & Structure
go syntaxgo
func NewReaderDict(r io.Reader, dict []byte) (io.ReadCloser, error)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| r | io.Reader | Compressed input stream. Implement io.ByteReader to avoid unnecessary read-ahead where applicable. |
| dict | []byte | Preset dictionary bytes; use matching dictionary when decoding. |
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 dictPack(dict []byte) []byte { var b bytes.Buffer w, e := zlib.NewWriterLevelDict(&b, zlib.BestCompression, dict) must(e) _, e = w.Write([]byte("Go")) must(e) must(w.Close()) return b.Bytes()}func main() { d := []byte("Go Go Go") r, e := zlib.NewReaderDict(bytes.NewReader(dictPack(d)), d) must(e) p, e := io.ReadAll(r) must(e) must(r.Close()) fmt.Println(string(p))}