Standard library / compress/zlib
compress/zlib.NewWriterLevelDict()
Create a ZLIB compressor using a preset dictionary. The receiver needs the same dictionary; use ordinary ZLIB when dictionary distribution is not coordinated. Expected stdout: 'Go\n'.
Formal Syntax & Structure
go syntaxgo
func NewWriterLevelDict(w io.Writer, level int, dict []byte) (*Writer, error)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| w | io.Writer | Destination for compressed bytes; closing the compressor does not close this writer. |
| level | int | Compression level from the package constants or supported integer range. |
| dict | []byte | Preset dictionary bytes; use matching dictionary when decoding. |
Return Value
(*Writer, error)
New compression writer or an error for invalid settings.
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))}