Help us shape the future of Go! Take the 2026 Go Developer Survey

compression

package
v2.17.1 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2026 License: AGPL-3.0 Imports: 6 Imported by: 4

Documentation

Overview

Package compression provides data compression and decompression using Zstd and S2 algorithms.

Example (RoundTrip)
package main

import (
	"fmt"

	"github.com/primandproper/primitives-go/v2/compression"
)

func main() {
	c, err := compression.NewCompressor("zstd")
	if err != nil {
		panic(err)
	}

	original := []byte("hello, world!")
	compressed, err := c.CompressBytes(original)
	if err != nil {
		panic(err)
	}

	decompressed, err := c.DecompressBytes(compressed)
	if err != nil {
		panic(err)
	}

	fmt.Println(string(decompressed))
}
Output:
hello, world!

Index

Examples

Constants

View Source
const (
	// AlgorithmZstd selects the Zstandard compression algorithm.
	AlgorithmZstd Algorithm = "zstd"
	// AlgorithmS2 selects the S2 compression algorithm.
	AlgorithmS2 Algorithm = "s2"

	// DefaultMaxDecompressedBytes bounds how many bytes DecompressBytes will produce for a
	// single input, guarding against decompression bombs (a small hostile payload that expands
	// to gigabytes). It matches zstd's own default decoder memory limit. Override per-Compressor
	// with WithMaxDecompressedBytes.
	DefaultMaxDecompressedBytes uint64 = 64 << 20 // 64 MiB
)

Variables

View Source
var (
	// ErrInvalidAlgorithm is returned when an unsupported compression algorithm is requested.
	//
	// Algorithm is a runtime-config selection seam like any provider name, so this
	// wraps errors.ErrUnknownProvider: startup code that branches on one sentinel
	// for "the config named something this build does not have" catches this too,
	// and a caller that wants to say "compression" specifically still can.
	ErrInvalidAlgorithm = errors.Wrap(errors.ErrUnknownProvider, "invalid compression algorithm")
	// ErrDecompressedTooLarge is returned when decompressing an input would exceed the
	// configured maximum decompressed size.
	ErrDecompressedTooLarge = errors.New("decompressed output exceeds configured maximum")
)

Functions

This section is empty.

Types

type Algorithm

type Algorithm string

Algorithm identifies a supported compression algorithm. It is a named string type so callers can select an algorithm from a runtime config string via a plain conversion (e.g. Algorithm(cfg.Algorithm)).

type Compressor

type Compressor interface {
	CompressBytes(in []byte) ([]byte, error)
	DecompressBytes(in []byte) ([]byte, error)
}

Compressor compresses and decompresses byte slices.

type Option

type Option func(*StandardCompressor)

Option configures a Compressor.

func WithMaxDecompressedBytes

func WithMaxDecompressedBytes(n uint64) Option

WithMaxDecompressedBytes overrides the maximum number of bytes DecompressBytes will produce for a single input. A value of 0 leaves the default (DefaultMaxDecompressedBytes) in place.

type StandardCompressor

type StandardCompressor struct {
	// contains filtered or unexported fields
}

StandardCompressor is the Compressor for every supported Algorithm. It is exported, and returned by NewCompressor, so a caller can depend on the compressor it built rather than on the Compressor seam.

func NewCompressor

func NewCompressor(a Algorithm, opts ...Option) (*StandardCompressor, error)

NewCompressor returns a new Compressor for the given Algorithm. An unknown or empty Algorithm yields ErrInvalidAlgorithm.

func (*StandardCompressor) CompressBytes

func (c *StandardCompressor) CompressBytes(in []byte) ([]byte, error)

func (*StandardCompressor) DecompressBytes

func (c *StandardCompressor) DecompressBytes(in []byte) ([]byte, error)

Jump to

Keyboard shortcuts

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