bencode

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

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

Go to latest
Published: Nov 1, 2020 License: GPL-3.0 Imports: 6 Imported by: 0

README

bencode

Package bencode implements encoding and decoding of the Bencode serialization format. Bencode is used by the peer-to-peer file sharing system BitTorrent.

Build Status

Installation

To install this package, run:

$ go get github.com/aryann/bencode

Usage

The following is an example that shows how to encode and decode values using this library. This example is also available at the Go Playground.

package main

import (
	"fmt"
	"log"

	"github.com/aryann/bencode"
)

func main() {
	type MyData struct {
		MyString   string `bencode:"my-string"`
		MyIntegers []int64  `bencode:"my-integers"`
	}

	myData := MyData{
		MyString:   "Hello, world!",
		MyIntegers: []int64{1, 22, 333},
	}
	encoded, err := bencode.Marshal(myData)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("Encoded data: %s\n", string(encoded))

	var myDecodedData MyData
	if err := bencode.Unmarshal(encoded, &myDecodedData); err != nil {
		log.Fatal(err)
	}
	fmt.Printf("Decoded data: %+v\n", myDecodedData)
}

Running this example produces the following output:

Encoded data: d11:my-integersli1ei22ei333ee9:my-string13:Hello, world!e
Decoded data: {MyString:Hello, world! MyIntegers:[1 22 333]}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Marshal

func Marshal(v interface{}) ([]byte, error)

Marshal returns a bencode encoding of v.

func Unmarshal

func Unmarshal(data []byte, v interface{}) error

Unmarshal deserializes a Bencode string.

Types

This section is empty.

Jump to

Keyboard shortcuts

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