Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Marshal ¶
Marshal returns bencode encoding of v.
Example ¶
package main import ( "bytes" "fmt" "github.com/cristalhq/bencode" ) func main() { // data to process, most of the types are supported var data any = map[string]any{ "1": 42, "hello": "world", "foo": []string{"bar", "baz"}, } buf, err := bencode.Marshal(data) checkErr(err) fmt.Printf("marshaled: %s\n", string(buf)) // or via Encoder: w := &bytes.Buffer{} // or any other io.Writer err = bencode.NewEncoder(w).Encode(data) checkErr(err) } func checkErr(err error) { if err != nil { panic(err) } }
Output: marshaled: d1:1i42e3:fool3:bar3:baze5:hello5:worlde
func MarshalTo ¶
MarshalTo returns bencode encoding of v written to dst.
Example ¶
package main import ( "fmt" "github.com/cristalhq/bencode" ) func main() { var data any = map[string]any{ "1": 42, "hello": "world", "foo": []string{"bar", "baz"}, } buf := make([]byte, 0, 128) buf, err := bencode.MarshalTo(buf, data) checkErr(err) fmt.Printf("marshaled: %s\n", string(buf)) } func checkErr(err error) { if err != nil { panic(err) } }
Output: marshaled: d1:1i42e3:fool3:bar3:baze5:hello5:worlde
func Unmarshal ¶
Unmarshal parses the bencoded data and stores the result in the value pointed to by v.
Example ¶
package main import ( "bytes" "fmt" "github.com/cristalhq/bencode" ) func main() { var data any buf := []byte("li1ei42ee") err := bencode.Unmarshal(buf, &data) checkErr(err) // or via Decoder: r := bytes.NewBufferString("li1ei42ee") // or any other io.Reader err = bencode.NewDecoder(r).Decode(&data) checkErr(err) fmt.Printf("unmarshaled: %v\n", data) } func checkErr(err error) { if err != nil { panic(err) } }
Output: unmarshaled: [1 42]
Types ¶
type A ¶ added in v0.4.0
type A []any
A is a Bencode array.
Example:
bencode.A{"hello", "world", 3.14159, bencode.D{{"foo", 12345}}}
type D ¶ added in v0.4.0
type D []e
D is an ordered representation of a Bencode document.
Example usage:
bencode.D{{"hello", "world"}, {"foo", "bar"}, {"pi", 3.14159}}
type Decoder ¶
type Decoder struct {
// contains filtered or unexported fields
}
A Decoder reads and decodes Bencode values from an input stream.
func NewDecodeBytes ¶
NewDecodeBytes returns a new decoder that decodes given bytes.
func NewDecoder ¶
NewDecoder returns a new decoder that reads from r.
type Encoder ¶
type Encoder struct {
// contains filtered or unexported fields
}
An Encoder writes Bencode values to an output stream.
func NewEncoder ¶
NewEncoder returns a new encoder that writes to w.
func NewEncoderWithBuffer ¶
NewEncoderWithBuffer returns a new encoder that writes to w.
type M ¶ added in v0.4.0
M is an unordered representation of a Bencode document.
Example usage:
bencode.M{"hello": "world", "foo": "bar", "pi": 3.14159}
type Marshaler ¶
Marshaler is the interface implemented by types that can marshal themselves into valid Bencode.
type Unmarshaler ¶
Unmarshaler is the interface implemented by types that can unmarshal a Bencode description of themselves.