I am completely new to go and am planning on building a bittorent client as my first project to learn the language. One aspect is econding and decoding of bittorrent files. An example that I've found creates a struct containing a bufio.Reader, and then uses some interfaces to add additional methods. Can someone explain the mechanism for constructing these interfaces in such a way that overloads the struct? I'm not clear on how the functions in this file are being added to the decoder struct (https://github.com/marksamman/bencode/blob/master/decoder.go):
package bencode
import (
"bufio"
"errors"
"io"
"strconv"
)
type decoder struct {
bufio.Reader
}
func (decoder *decoder) readDictionary() (map[string]interface{}, error) {
dict := make(map[string]interface{})
}
func (decoder *decoder) readString() (string, error) {
...
}
func (decoder *decoder) readList() ([]interface{}, error) {
...
}
func Decode(reader io.Reader) (map[string]interface{}, error) {
decoder := decoder{*bufio.NewReader(reader)}
if firstByte, err := decoder.ReadByte(); err != nil {
return make(map[string]interface{}), nil
} else if firstByte != 'd' {
return nil, errors.New("bencode data must begin with a dictionary")
}
return decoder.readDictionary()
}