bencode

package module
v0.0.0-...-dc84f26 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 21, 2015 License: MIT Imports: 7 Imported by: 25

README

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Decode

func Decode(reader io.Reader) (map[string]interface{}, error)

Decode takes an io.Reader and parses it as bencode, on failure, err will be a non-nil value

func Encode

func Encode(v interface{}) []byte

Encode takes a bencode supported data type and returns a bencode byte array representation

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL