Standard library / compress/flate

compress/flate.NewWriterDict()

Create a raw DEFLATE writer with a preset history dictionary. The raw format does not carry a dictionary identifier; the caller must coordinate the dictionary. Expected stdout: 'Go\n'.

Formal Syntax & Structure

go syntaxgo
func NewWriterDict(w io.Writer, level int, dict []byte) (*Writer, error)

Parameters & Attributes

NameTypeDescription
wio.WriterDestination for compressed bytes; closing the compressor does not close this writer.
levelintCompression level from the package constants or supported integer range.
dict[]bytePreset 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/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))}
Go Reference