A Go language binding for encoding and decoding data in the bencode format that is used by the BitTorrent peer-to-peer file sharing protocol.
go get -u github.com/jackpal/bencode-goimport bencode "github.com/jackpal/bencode-go"data := myAwesomeObject{}
err := bencode.Unmarshal(reader, &data)data, err := bencode.Decode(reader)dec := bencode.NewDecoder(reader).
SetMaxStringLength(10 * 1024 * 1024). // 10 MiB
SetMaxDepth(50).
SetStrict(true) // Enforces BEP 3 rules (no leading zeros, sorted keys, etc.)
data, err := dec.Decode()
// or: err := dec.Unmarshal(&myObject)
// If the stream contains trailing non-bencode data, access unread buffered bytes:
leftover := dec.Buffered()type Torrent struct {
Announce string `bencode:"announce"`
Info bencode.RawMessage `bencode:"info"`
}
var t Torrent
err := bencode.Unmarshal(reader, &t)
// Compute the SHA-1 info_hash directly from the raw bencoded info dictionary:
infoHash := sha1.Sum(t.Info)err := bencode.Marshal(writer, data)http://godoc.org/github.com/jackpal/bencode-go
This project is licensed under the Go Authors standard license. (See the LICENSE file for details.)
| tag | Notes |
|---|---|
| v1.2.0 | Added RawMessage support, Decoder.Buffered(), and skip unexported fields. |
| v1.1.0 | Added Decoder with configurable resource limits, strict BEP 3 validation, and any types. |
| v1.0.2 | Added go module. |
| v1.0.1 | Removed architecture specific test that was failing on ARM. |
| v1.0.0 | First version. |