Categorygithub.com/trim21/go-bencode
modulepackage
0.0.15
Repository: https://github.com/trim21/go-bencode.git
Documentation: pkg.go.dev

# README

go-bencode

GitHub tag (latest SemVer) Go Reference

Decoding and encoding bencode.

Support All go type including map/slice/struct/array, and simple type like bool/int/uint/string/....

float32 and float64 are not supported, bencode doesn't have this type.

Encoding and decoding some type from standard library like time.Time, net.IP are not supported. If you have any thought about how to support these types, please create an issue.

Or you can wrap these types and implement bencode.Marshaler or bencode.Unmarshaler

Supported and tested go version

  • 1.22

Install

go get github.com/trim21/go-bencode

Usage

See examples

Marshal

If you want to encode customize type as struct field with omitempty, do implement both bencode.Marshaler and bencode.IsZeroValue, so encoder could know if it's a empty value and skip fields.

Bencode doesn't have null type, so all struct field with pointer type(*T) will get omitempty by default.

Unmarshal

go any type will be decoded as map[string]any, []any, int64 or string.

[]uint8([]byte) and [N]uint8([N]byte) will be decoded as bencode string.

Decode Go string may not be valid utf8 string.

Go Array will be decoded with size check. Only bencode string/list with same length are valid, otherwise it will return a error.

Note

go reflect package allow you to create dynamic struct with reflect.StructOf, but please use it with caution.

For performance, this package will try to "compile" input type to a static encoder/decoder at first time and cache it for future use.

So a dynamic struct may cause memory leak.

License

MIT License

# Functions

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Structs

No description provided by the author

# Interfaces

IsZeroValue add support type implements Marshaler and omitempty var s struct { Field T `bencode:"field,omitempty"` } if `T` implements [Marshaler], it can implement [IsZeroValue] so bencode know if it's.
Marshaler allow users to implement its own encoder.
No description provided by the author

# Type aliases

No description provided by the author