Binary is a high-performance binary serialization library for Go, specifically designed for TinyGo compatibility and resource-constrained environments.
- TinyGo Compatible: Optimized for embedded systems and WebAssembly.
- Extreme Performance: Minimal allocations and efficient encoding.
- Simple API: Just
EncodeandDecode. - Field Skipping: Automatically skips private fields and respects
json:"-"orbinary:"-"tags. - Zero Dependencies: Core logic is lightweight and self-contained.
For detailed performance comparisons against the standard library and the impact of name-based optimization, see BENCHMARK.md.
go get github.com/tinywasm/binarypackage main
import (
"github.com/tinywasm/binary"
)
type User struct {
Name string // Included
Age int // Included
secret string // Skipped (private)
Ignored string `json:"-"` // Skipped (JSON tagged)
Hidden string `binary:"-"` // Skipped (Binary tagged)
}
// Optional: Implement this method to improve performance (avoids reflection)
func (u *User) HandlerName() string {
return "users"
}
func main() {
user := &User{
Name: "Alice",
Age: 30,
secret: "hidden",
Ignored: "tagged out",
Hidden: "binary skip",
}
// Encode
var data []byte
binary.Encode(user, &data)
// Decode (secret, Ignored and Hidden will remain empty)
var decoded User
binary.Decode(data, &decoded)
// Output: {Name:Alice Age:30 secret: Ignored: Hidden:}
}- Benchmarks - Performance comparisons
- Message Envelope - Inter-module communication format
Encode(input, output any) error: Encodes into*[]byteorio.Writer.Decode(input, output any) error: Decodes from[]byteorio.Reader.SetLog(fn func(...any)): Sets internal logger for debugging.
This project is an adaptation of Kelindar/binary focused on TinyGo.