Home

Awesome

borsh-go

Go Reference

borsh-go is an implementation of the [Borsh] binary serialization format for Go projects.

Borsh stands for Binary Object Representation Serializer for Hashing. It is meant to be used in security-critical projects as it prioritizes consistency, safety, speed, and comes with a strict specification.

Features

Usage

Example

package demo

import (
	"log"
	"reflect"
	"testing"

	"github.com/near/borsh-go"
)

type A struct {
	X uint64
	Y string
	Z string `borsh_skip:"true"` // will skip this field when serializing/deserializing
}

func TestSimple(t *testing.T) {
	x := A{
		X: 3301,
		Y: "liber primus",
	}
	data, err := borsh.Serialize(x)
	log.Print(data)
	if err != nil {
		t.Error(err)
	}
	y := new(A)
	err = borsh.Deserialize(y, data)
	if err != nil {
		t.Error(err)
	}
	if !reflect.DeepEqual(x, *y) {
		t.Error(x, y)
	}
}

For more examples of usage, refer to borsh_test.go.

Type Mappings

BorshGoDescription
boolbool
u8 integeruint8
u16 integeruint16
u32 integeruint32
u64 integeruint64
u128 integerbig.Int
i8 integerint8
i16 integerint16
i32 integerint32
i64 integerint64
i128 integerNot supported yet
f32 floatfloat32
f64 floatfloat64
fixed-size array[size]typego array
dynamic-size array[]typego slice
stringstring
option*typego pointer
mapmap
setmap[type]struct{}go map with value type set to struct{}
structsstruct
enumborsh.Enumuse type MyEnum borsh.Enum to define enum type