go-rencode is a Go implementation of aresch/rencode.
The rencode logic is similar to bencode. For complex, heterogeneous data structures with many small elements, r-encodings take up significantly less space than b-encodings.
Example of encoder construction and use:
b := bytes.Buffer{}
e := rencode.NewEncoder(&b)
err := e.Encode(100, true, "hello world", rencode.NewList(42, "nesting is awesome"), 3.14, rencode.Dictionary{})
You can use either specific methods to encode one of the supported types, or the interface-generic Encode()
method.
Example of decoder construction:
e := rencode.NewDecoder(&b)
The DecodeNext()
method can be used to decode the next value from the rencode stream; however this method returns an interface{}
while it is usually the norm that there is an expected type instead; in such cases, it is advised to use the Scan()
method instead,
which accepts a pointer to any of the supported types.
Example:
var i int
var b bool
var s string
var l rencode.List
err := e.Scan(&i, &b, &s, &l)
You can also decode a dictionary directly into a struct:
var s struct {
Alpha int
Beta string
Gamma float64 `rencode:"rencode-exclude"`
}
var d Dictionary
d.Add("alpha", int(54123))
d.Add("beta", "test")
err := d.ToStruct(&s, "rencode-exclude")
In this example every field/value of the dictionary must be mapped to a struct field, except those tagged as rencode-exclude
.
This works as well with nested dictionaries mapping to nested structs.
Only the following types are supported:
- rencode.List
- rencode.Dictionary
- big.Int (any integer with more than 63 bits of information)
- bool
- float32, float64
- []byte, string (all strings are stored as byte slices anyway)
- int8, int16, int32, int64, int
- uint8, uint16, uint32, uint64, uint
The rencode.List
and rencode.Dictionary
implement Python-alike features and can store values and keys of
the simpler types enumerated above.
- try using
reflect.Value
instead of the generated code
- This Go version: gdm85
- Original Python version: Petru Paler, Connelly Barnes et al.
- Cython version: Andrew Resch
go-rencode is licensed under GNU GPL v2, see COPYING for license information.