Skip to content

Latest commit

 

History

History
61 lines (47 loc) · 1.07 KB

README.md

File metadata and controls

61 lines (47 loc) · 1.07 KB

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"
)

func main() {
	dict := make(map[string]interface{})
	dict["string key"] = "hello world"
	dict["int key"] = 123456
	fmt.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"
)

func main() {
	file, err := os.Open(os.Args[1])
	if err != nil {
		log.Fatal(err)
	}
	defer file.Close()

	dict, err := bencode.Decode(file)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Printf("string: %s\n", dict["string key"].(string))
	fmt.Printf("int: %d\n", dict["int key"].(int64))
}