Skip to content

Repository files navigation

bencode Build Status

Bencode implementation in Go

Install

$ go get github.com/marksamman/bencode

Usage

Encode

bencode.Encode takes a map[string]interface{} as argument and returns a byte array. Example:

package main
import (
"fmt""github.com/marksamman/bencode"
)
funcmain() {
dict:=make(map[string]interface{})
dict["string key"] ="hello world"dict["int key"] =123456fmt.Printf("bencode encoded dict: %s\n", bencode.Encode(dict))
}

Decode

bencode.Decode takes an io.Reader as argument and returns (map[string]interface{}, error). Example:

package main
import (
"fmt""log""os""github.com/marksamman/bencode"
)
funcmain() {
file, err:=os.Open(os.Args[1])
iferr!=nil {
log.Fatal(err)
}
deferfile.Close()
dict, err:=bencode.Decode(file)
iferr!=nil {
log.Fatal(err)
}
fmt.Printf("string: %s\n", dict["string key"].(string))
fmt.Printf("int: %d\n", dict["int key"].(int64))
}

About

Bencode implementation in Go

Resources

Stars

62 stars

Watchers

2 watching

Forks

Releases

Packages

Contributors

Languages