https://github.com/riptl/go-binary
Encoding/decoding in Borsh and other formats.
https://github.com/riptl/go-binary
Last synced: 2 months ago
JSON representation
Encoding/decoding in Borsh and other formats.
- Host: GitHub
- URL: https://github.com/riptl/go-binary
- Owner: riptl
- License: apache-2.0
- Fork: true (gagliardetto/binary)
- Created: 2022-02-27T18:46:04.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2022-08-21T06:35:02.000Z (over 3 years ago)
- Last Synced: 2025-10-13T21:25:52.804Z (5 months ago)
- Language: Go
- Homepage:
- Size: 227 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# binary
### Borsh
#### Decoding borsh
```golang
dec := bin.NewBorshDecoder(data)
var meta token_metadata.Metadata
err = dec.Decode(&meta)
if err != nil {
panic(err)
}
```
#### Encoding borsh
```golang
buf := new(bytes.Buffer)
enc := bin.NewBorshEncoder(buf)
err := enc.Encode(meta)
if err != nil {
panic(err)
}
// fmt.Print(buf.Bytes())
```
### Optional Types
```golang
type Person struct {
Name string
Age uint8 `bin:"optional"`
}
```
Rust equivalent:
```rust
struct Person {
name: String,
age: Option
}
```
### Enum Types
```golang
type MyEnum struct {
Enum bin.BorshEnum `borsh_enum:"true"`
One bin.EmptyVariant
Two uint32
Three int16
}
```
Rust equivalent:
```rust
enum MyEnum {
One,
Two(u32),
Three(i16),
}
```
### Exported vs Unexported Fields
In this example, the `two` field will be skipped by the encoder/decoder because the
field is not exported.
```golang
type MyStruct struct {
One string
two uint32
Three int16
}
```
### Skip Decoding/Encoding Attributes
Encoding/Decoding of exported fields can be skipped using the `borsh_skip` tag.
```golang
type MyStruct struct {
One string
Two uint32 `borsh_skip:"true"`
Three int16
}
```