Skip to content

Codec Reference

All codecs listed below implement Codec[T], StreamCodec[T], or both. The core module packages use only the Go standard library.

Serialization Codecs

PackageConstructorTypeStreamCodec
jsonjson.NewCodec[T]()generic Tjson.NewStreamCodec[T]()
xmlxml.NewCodec[T]()generic Txml.NewStreamCodec[T]()
gobgob.NewCodec[T]()generic Tgob.NewStreamCodec[T]()
asn1asn1.NewCodec[T]()generic Tasn1.NewStreamCodec[T]()
csvcsv.NewCodec()[][]stringcsv.NewStreamCodec()
pempem.NewCodec()*pem.Blockpem.NewStreamCodec()

Binary Encoding Codecs

PackageConstructorTypeStreamCodec
base64base64.NewCodec()[]bytebase64.NewStreamCodec()
base32base32.NewCodec()[]bytebase32.NewStreamCodec()
hexhex.NewCodec()[]bytehex.NewStreamCodec()
ascii85ascii85.NewCodec()[]byteascii85.NewStreamCodec()

Compression Wrappers

Compression codecs wrap an inner Codec[T] or StreamCodec[T] using the decorator pattern.

PackageConstructorOptions
gzipgzip.NewCodec[T](codec, opts...)gzip.WithLevel(int)
flateflate.NewCodec[T](codec, opts...)flate.WithLevel(int)
snappysnappy.NewCodec[T](codec)
zstdzstd.NewCodec[T](codec, opts...)zstd.WithLevel(zstd.EncoderLevel)
brotlibrotli.NewCodec[T](codec, opts...)brotli.WithLevel(int)

Each also has a stream variant: gzip.NewStreamCodec[T](streamCodec, opts...), etc.

TIP

snappy, zstd, and brotli are submodule packages that require a separate go get.

Utility

File Codec

The file package wraps any codec to read/write files atomically (temp file + rename).

go
file.NewCodec[T](codec, opts...)       // Encode(path string, v T) error
file.NewStreamCodec[T](codec, opts...) // Encode(path string, v T) error

Options: file.WithPermissions(os.FileMode) — default 0o644.

WARNING

The file codec has a different method signature — it uses path string instead of []byte or io.Writer. It does not satisfy the Codec[T] or StreamCodec[T] interfaces. See File Codec for details.

Submodule Packages

These packages have external dependencies and live in separate Go modules. Install them individually.

PackageImport PathDependencyStreamCodec
json2github.com/foomo/goencode/json2go-json-experiment— (has EncodeTo/DecodeFrom methods)
yaml/v2github.com/foomo/goencode/yaml/v2go.yaml.in/yaml/v2
yaml/v3github.com/foomo/goencode/yaml/v3go.yaml.in/yaml/v3
yaml/v4github.com/foomo/goencode/yaml/v4go.yaml.in/yaml/v4
snappygithub.com/foomo/goencode/snappygithub.com/golang/snappysnappy.NewStreamCodec[T](codec)
brotligithub.com/foomo/goencode/brotligithub.com/andybalholm/brotlibrotli.NewStreamCodec[T](codec, opts...)
tomlgithub.com/foomo/goencode/tomlgithub.com/BurntSushi/tomltoml.NewStreamCodec[T]()
zstdgithub.com/foomo/goencode/zstdgithub.com/klauspost/compresszstd.NewStreamCodec[T](codec, opts...)