packer

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2025 License: Apache-2.0 Imports: 5 Imported by: 3

README

Packer

Packer is a simple, efficient, and lightweight Go library for binary serialization. It provides a straightforward API to encode various Go data types into a byte stream and decode them back, making it ideal for network protocols, file storage, or any application requiring compact data representation.

The library is built for performance and ease of use, offering both struct-based encoder/decoder workflows and standalone functions for granular control.


Features

  • Wide Type Support: Natively handles bool, (u)int8-64, float32/64, string, []byte, and *bytes.Buffer.
  • Consistent Endianness: All multi-byte numeric types are encoded and decoded using Big Endian byte order for cross-platform compatibility.
  • Extensible: Easily support custom data structures by implementing the Encodeable and Decodable interfaces.
  • Performance-Oriented: Includes optimizations like a zero-copy []byte to string conversion using unsafe.
  • Simple Streaming API: The BinaryEncoder and BinaryDecoder structs operate on a continuous stream of bytes, making it easy to serialize and deserialize complex sequences of data.

Installation

go get github.com/FrogoAI/packer

Usage

The core of the library is the BinaryEncoder for writing data and the BinaryDecoder for reading it. You can encode multiple values sequentially and then decode them in the same order.

Here is a basic example:

package main

import (
	"fmt"
	"github.com/FrogoAI/packer"
)

func main() {
	// 1. Create a new encoder
	encoder := packer.NewBinaryEncoder()

	// 2. Encode a sequence of different data types
	_ = encoder.Encode(uint32(123456))
	_ = encoder.Encode("Hello, Packer!")
	_ = encoder.Encode(float64(3.14159))
	_ = encoder.Encode(true)
	_ = encoder.Encode([]byte{0xDE, 0xAD, 0xBE, 0xEF})

	// 3. Get the resulting byte slice
	encodedData := encoder.Bytes()
	fmt.Printf("Encoded Data: %x\n", encodedData)

	// 4. Create a decoder with the encoded data
	decoder := packer.NewBinaryDecoder(encodedData)

	// 5. Decode the values back into variables in the same order
	var myUint32 uint32
	var myString string
	var myFloat64 float64
	var myBool bool
	var myBytes []byte

	_ = decoder.Decode(&myUint32)
	_ = decoder.Decode(&myString)
	_ = decoder.Decode(&myFloat64)
	_ = decoder.Decode(&myBool)
	_ = decoder.Decode(&myBytes)

	// 6. Verify the results
	fmt.Printf("Decoded uint32: %d\n", myUint32)
	fmt.Printf("Decoded string: %s\n", myString)
	fmt.Printf("Decoded float64: %f\n", myFloat64)
	fmt.Printf("Decoded bool: %t\n", myBool)
	fmt.Printf("Decoded bytes: %x\n", myBytes)
}
Output:
Encoded Data: 0001e2400000000e48656c6c6f2c205061636b657221400921f9f01b866e0100000004deadbeef
Decoded uint32: 123456
Decoded string: Hello, Packer!
Decoded float64: 3.141590
Decoded bool: true
Decoded bytes: deadbeef

Performance

Benchmarks from binary_test.go show the performance characteristics of the library. The standalone functions (DecodeString, EncodeString) offer slightly different performance profiles due to reduced overhead compared to the struct-based methods.

Benchmark Operation Nanoseconds per Operation (ns/op)
BenchmarkBinaryEncoder (struct) 69.00 ns/op
BenchmarkEncode (standalone) 71.73 ns/op
BenchmarkBinaryDecoder (struct) 37.94 ns/op
BenchmarkDecode (standalone) 22.54 ns/op

License

This project is licensed under the MIT License. See the LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrWrongEncodeType = errors.New("error wrong encode type")
	ErrWrongDecodeType = errors.New("error wrong decode type")
)

All kind of errors

Functions

func ByteSliceToString

func ByteSliceToString(b []byte) string

ByteSliceToString cast given bytes to string, without allocation memory

func DecodeBool

func DecodeBool(buffer *bytes.Buffer) bool

DecodeBool decode to bool

func DecodeBuffer

func DecodeBuffer(buffer *bytes.Buffer) *bytes.Buffer

DecodeBuffer decode to buffer

func DecodeBytes

func DecodeBytes(buffer *bytes.Buffer) []byte

DecodeBytes decode to bytes

func DecodeDecodable

func DecodeDecodable(decodable Decodable, buffer *bytes.Buffer) any

DecodeDecodable decode specific structure

func DecodeFloat32

func DecodeFloat32(buffer *bytes.Buffer) float32

DecodeFloat32 decode to float32

func DecodeFloat64

func DecodeFloat64(buffer *bytes.Buffer) float64

DecodeFloat64 decode to float64

func DecodeInt

func DecodeInt(buffer *bytes.Buffer) int

DecodeInt decode to int

func DecodeInt8

func DecodeInt8(buffer *bytes.Buffer) int8

DecodeInt8 decode to int8

func DecodeInt16

func DecodeInt16(buffer *bytes.Buffer) int16

DecodeInt16 decode to int16

func DecodeInt32

func DecodeInt32(buffer *bytes.Buffer) int32

DecodeInt32 decode to int32

func DecodeInt64

func DecodeInt64(buffer *bytes.Buffer) int64

DecodeInt64 decode to int64

func DecodeString

func DecodeString(buffer *bytes.Buffer) string

DecodeString decode to string

func DecodeUint8

func DecodeUint8(buffer *bytes.Buffer) uint8

DecodeUint8 decode to uint8

func DecodeUint16

func DecodeUint16(buffer *bytes.Buffer) uint16

DecodeUint16 decode to uint16

func DecodeUint32

func DecodeUint32(buffer *bytes.Buffer) uint32

DecodeUint32 decode to uint32

func DecodeUint64

func DecodeUint64(buffer *bytes.Buffer) uint64

DecodeUint64 decode to uint64

func EncodeBool

func EncodeBool(v bool, buffer *bytes.Buffer)

EncodeBool encode bool

func EncodeBuffer

func EncodeBuffer(v *bytes.Buffer, buffer *bytes.Buffer)

EncodeBuffer encode buffer

func EncodeBytes

func EncodeBytes(v []byte, buffer *bytes.Buffer)

EncodeBytes encode bytes

func EncodeEncodeable

func EncodeEncodeable(v Encodeable, buffer *bytes.Buffer)

EncodeEncodeable encode Encodeable

func EncodeFloat32

func EncodeFloat32(v float32, buffer *bytes.Buffer)

EncodeFloat32 encode float32

func EncodeFloat64

func EncodeFloat64(v float64, buffer *bytes.Buffer)

EncodeFloat64 encode float64

func EncodeInt

func EncodeInt(v int, buffer *bytes.Buffer)

EncodeInt encode int

func EncodeInt8

func EncodeInt8(v int8, buffer *bytes.Buffer)

EncodeInt8 encode int8

func EncodeInt16

func EncodeInt16(v int16, buffer *bytes.Buffer)

EncodeInt16 encode int16

func EncodeInt32

func EncodeInt32(v int32, buffer *bytes.Buffer)

EncodeInt32 encode int32

func EncodeInt64

func EncodeInt64(v int64, buffer *bytes.Buffer)

EncodeInt64 encode int64

func EncodeString

func EncodeString(v string, buffer *bytes.Buffer)

EncodeString encode string

func EncodeUint8

func EncodeUint8(v uint8, buffer *bytes.Buffer)

EncodeUint8 encode uint8

func EncodeUint16

func EncodeUint16(v uint16, buffer *bytes.Buffer)

EncodeUint16 encode uint16

func EncodeUint32

func EncodeUint32(v uint32, buffer *bytes.Buffer)

EncodeUint32 encode uint32

func EncodeUint64

func EncodeUint64(v uint64, buffer *bytes.Buffer)

EncodeUint64 encode uint64

Types

type BinaryDecoder

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

BinaryDecoder contains buffer and convert binary to value

func NewBinaryDecoder

func NewBinaryDecoder(data []byte) *BinaryDecoder

NewBinaryDecoder return new decoder

func (*BinaryDecoder) Bytes

func (b *BinaryDecoder) Bytes() []byte

Bytes return bytes

func (*BinaryDecoder) Decode

func (b *BinaryDecoder) Decode(value any) error

Decode read value to bytes

type BinaryEncoder

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

BinaryEncoder contains buffer and convert values to binary

func NewBinaryEncoder

func NewBinaryEncoder() *BinaryEncoder

NewBinaryEncoder return new encoder

func (*BinaryEncoder) Bytes

func (b *BinaryEncoder) Bytes() []byte

Bytes return bytes

func (*BinaryEncoder) Encode

func (b *BinaryEncoder) Encode(value any) error

Encode add value to bytes

type Decodable

type Decodable interface {
	Decode(uint8, []byte) any
}

Decodable describe decodable interface

type Encodeable

type Encodeable interface {
	Encode() (uint8, []byte)
}

Encodeable describe encodeable interface

Jump to

Keyboard shortcuts

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