Fork me on GitHub

io.Reader

->

struct

dec := bencode.NewDecoder(r)
var torrent struct {
	Announce string
	List     [][]string `bencode:"announce-list"`
}
if err := dec.Decode(&torrent); err != nil {
	panic(err)
}

io.Reader

->

interface{}

dec := bencode.NewDecoder(r)
var torrent interface{}
if err := dec.Decode(&torrent); err != nil {
	panic(err)
}

string

->

interface{}

var torrent interface{}
if err := bencode.DecodeString(data, &torrent); err != nil {
	panic(err)
}

interface{}

->

io.Writer

var torrent interface{}
/* fill torrent with data */
enc := bencode.NewEncoder(w)
if err := enc.Encode(torrent); err != nil {
	panic(err)
}

struct

->

io.Writer

var x struct {
	Foo string
	Bar []string `bencode:"name"`
}
/* fill x with data */
enc := bencode.NewEncoder(w)
if err := enc.Encode(x); err != nil {
	panic(err)
}

interface{}

->

string

var torrent interface{}
/* fill torrent with data */
data, err := bencode.EncodeString(torrent)
if err != nil {
	panic(err)
}
fmt.Println(data)

Overview

Package bencode implements encoding and decoding of bencoded objects.

import "github.com/zeebo/bencode"

func DecodeString

func DecodeString(in string, val interface{}) error

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.

func EncodeString

func EncodeString(val interface{}) (string, error)

EncodeString returns the bencoded data of val as a string.

type Decoder

A Decoder reads and decodes bencoded data from an input stream.

type Decoder struct {
    // contains filtered or unexported fields
}

func NewDecoder

func NewDecoder(r io.Reader) *Decoder

NewDecoder returns a new decoder that reads from r

func (*Decoder) Decode

func (d *Decoder) Decode(val interface{}) error

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

type Encoder

An Encoder writes bencoded objects to an output stream.

type Encoder struct {
    // contains filtered or unexported fields
}

func NewEncoder

func NewEncoder(w io.Writer) *Encoder

NewEncoder returns a new encoder that writes to w.

func (*Encoder) Encode

func (e *Encoder) Encode(val interface{}) error

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.

type RawMessage []byte

RawMessage is a special type that will store the raw bencode data when encoding or decoding.