Standard library / compress/flate
compress/flate.NewReaderDict()
Create a raw DEFLATE reader using the same preset dictionary as the encoder. A wrong dictionary may lead to errors or incorrect output; raw DEFLATE is not authenticated. Expected stdout: 'Go\n'.
Formal Syntax & Structure
go syntaxgo
func NewReaderDict(r io.Reader, dict []byte) io.ReadCloserParameters & 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
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 dictPack(dict []byte) []byte { var b bytes.Buffer w, e := flate.NewWriterDict(&b, flate.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 := flate.NewReaderDict(bytes.NewReader(dictPack(d)), d) p, e := io.ReadAll(r) must(e) must(r.Close()) fmt.Println(string(p))}