zstd

package module
v0.0.0-...-f7050a3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 21, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewCodec

func NewCodec(opts ...Option) encoding.Codec[[]byte, []byte]

NewCodec returns a Zstandard compression codec. It is safe for concurrent use.

Example
package main

import (
	"fmt"

	goencode "github.com/foomo/goencode"
	json "github.com/foomo/goencode/json/v1"
	"github.com/foomo/goencode/zstd"
)

func main() {
	type Data struct {
		Name string
	}

	c := goencode.PipeCodec(json.NewCodec[Data](), zstd.NewCodec())

	encoded, err := c.Encode(Data{Name: "example-123"})
	if err != nil {
		fmt.Printf("Encode failed: %v\n", err)
		return
	}

	var decoded Data
	if err := c.Decode(encoded, &decoded); err != nil {
		fmt.Printf("Decode failed: %v\n", err)
		return
	}

	fmt.Printf("Decoded Name: %s\n", decoded.Name)
}
Output:
Decoded Name: example-123

func NewDecoder

func NewDecoder(opts ...Option) encoding.Decoder[[]byte, []byte]

NewDecoder returns a Zstandard decompression decoder.

func NewEncoder

func NewEncoder(opts ...Option) encoding.Encoder[[]byte, []byte]

NewEncoder returns a Zstandard compression encoder.

func NewStreamCodec

func NewStreamCodec(opts ...Option) encoding.StreamCodec[[]byte]

NewStreamCodec returns a Zstandard compression stream codec. It is safe for concurrent use.

Example
package main

import (
	"bytes"
	"fmt"

	"github.com/foomo/goencode/zstd"
)

func main() {
	c := zstd.NewStreamCodec()

	input := []byte("hello zstd stream")

	var buf bytes.Buffer
	if err := c.Encode(&buf, input); err != nil {
		fmt.Printf("Encode failed: %v\n", err)
		return
	}

	var decoded []byte
	if err := c.Decode(&buf, &decoded); err != nil {
		fmt.Printf("Decode failed: %v\n", err)
		return
	}

	fmt.Printf("Decoded: %s\n", string(decoded))
}
Output:
Decoded: hello zstd stream

func NewStreamDecoder

func NewStreamDecoder(opts ...Option) encoding.StreamDecoder[[]byte]

NewStreamDecoder returns a Zstandard decompression stream decoder.

func NewStreamEncoder

func NewStreamEncoder(opts ...Option) encoding.StreamEncoder[[]byte]

NewStreamEncoder returns a Zstandard compression stream encoder.

Types

type Option

type Option func(o *options)

Option configures a zstd Codec.

func WithLevel

func WithLevel(level zstd.EncoderLevel) Option

WithLevel sets the Zstandard compression level. Use zstd constants: zstd.SpeedFastest, zstd.SpeedDefault, zstd.SpeedBetterCompression, zstd.SpeedBestCompression.

func WithMaxDecodedSize

func WithMaxDecodedSize(n int64) Option

WithMaxDecodedSize sets the maximum allowed size of decompressed data in bytes. If the decompressed data exceeds this limit, Decode returns an error. A value of 0 (the default) means no limit.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL