Categorygithub.com/eteu-technologies/borsh-go
repositorypackage
0.3.2
Repository: https://github.com/eteu-technologies/borsh-go.git
Documentation: pkg.go.dev

# README

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

  • Based on Go Reflection. Avoids the need for create protocol file and code generation. Simply defining struct and go.

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
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