Skip to content

tinywasm/binary

Repository files navigation

Binary

Project Badges

Binary is a high-performance binary serialization library for Go, specifically designed for TinyGo compatibility and resource-constrained environments.

Features

  • TinyGo Compatible: Optimized for embedded systems and WebAssembly.
  • Extreme Performance: Minimal allocations and efficient encoding.
  • Simple API: Just Encode and Decode.
  • Field Skipping: Automatically skips private fields and respects json:"-" or binary:"-" tags.
  • Zero Dependencies: Core logic is lightweight and self-contained.

Benchmarks

For detailed performance comparisons against the standard library and the impact of name-based optimization, see BENCHMARK.md.

Installation

go get github.com/tinywasm/binary

Quick Start

package 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:}
}

Documentation

API

  • Encode(input, output any) error: Encodes into *[]byte or io.Writer.
  • Decode(input, output any) error: Decodes from []byte or io.Reader.
  • SetLog(fn func(...any)): Sets internal logger for debugging.

License MIT

This project is an adaptation of Kelindar/binary focused on TinyGo.

About

Binary is a high-performance binary serialization library for Go, specifically designed for TinyGo compatibility

Topics

Resources

Stars

Watchers

Forks

Sponsor this project

 

Packages

 
 
 

Contributors

Languages