Documentation ¶
Overview ¶
Package bencode implements encoding and decoding of bencoded objects.
It has a similar API to the encoding/json package and many other serialization formats.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DecodeBytes ¶
DecodeBytes reads the data in b and stores it into the value pointed to by val. Read the docs for Decode for more information.
Example ¶
var torrent interface{} if err := DecodeBytes([]byte(data), &torrent); err != nil { panic(err) }
Output:
func DecodeString ¶
DecodeString reads the data in the string and stores it into the value pointed to by val. Read the docs for Decode for more information.
Example ¶
var torrent interface{} if err := DecodeString(data, &torrent); err != nil { panic(err) }
Output:
Types ¶
type Decoder ¶
type Decoder struct {
// contains filtered or unexported fields
}
A Decoder reads and decodes bencoded data from an input stream.
func NewDecoder ¶
NewDecoder returns a new decoder that reads from r
func (*Decoder) BytesParsed ¶
BytesParsed returns the number of bytes that have actually been parsed
func (*Decoder) Decode ¶
Decode reads the bencoded value from its input and stores it in the value pointed to by val. Decode allocates maps/slices as necessary with the following additional rules: To decode a bencoded value into a nil interface value, the type stored in the interface value is one of:
int64 for bencoded integers string for bencoded strings []interface{} for bencoded lists map[string]interface{} for bencoded dicts
Example ¶
dec := NewDecoder(r) var torrent struct { Announce string List [][]string `bencode:"announce-list"` } if err := dec.Decode(&torrent); err != nil { panic(err) }
Output:
type Encoder ¶
type Encoder struct {
// contains filtered or unexported fields
}
An Encoder writes bencoded objects to an output stream.
func NewEncoder ¶
NewEncoder returns a new encoder that writes to w.
func (*Encoder) Encode ¶
Encode writes the bencoded data of val to its output stream. See the documentation for Decode about the conversion of Go values to bencoded data.
Example ¶
var x struct { Foo string Bar []string `bencode:"name"` } enc := NewEncoder(w) if err := enc.Encode(x); err != nil { panic(err) }
Output: