torrent/bencode
guoguangwu 7036f221f4 chore: remove refs to deprecated io/ioutil 2023-07-10 00:20:13 +10:00
..
testdata Add failing bencode fuzz input 2021-12-12 16:56:01 +11:00
README.md The bencode README is markdown 2017-09-12 18:31:56 +10:00
api.go Don't panic on int parse failures 2021-08-12 13:46:02 +10:00
bench_test.go gorond test files 2023-05-12 13:47:47 +10:00
both_test.go chore: remove refs to deprecated io/ioutil 2023-07-10 00:20:13 +10:00
bytes.go Add bencode.Bytes.GoString 2022-03-09 20:57:23 +11:00
bytes_test.go bencode.Bytes: Tests and stricter checks 2021-11-02 17:28:05 +11:00
decode.go bencode: Only use unsafe.String for go>=1.20 2023-04-03 15:10:38 +10:00
decode_test.go bencode: Support parsing strings into bool 2023-03-20 10:50:22 +11:00
encode.go gofumpt 2021-11-08 14:47:01 +11:00
encode_test.go bencode: Encode arrays of bytes as strings 2021-10-28 16:21:23 +11:00
fuzz_test.go Add bencode FuzzInterfaceRoundTrip 2021-12-12 16:56:01 +11:00
misc.go bencode: Only use unsafe.String for go>=1.20 2023-04-03 15:10:38 +10:00
scanner.go bencode.scanner.ReadByte returned errors when it shouldn't have 2017-11-10 23:44:24 +11:00
string.go bencode: Only use unsafe.String for go>=1.20 2023-04-03 15:10:38 +10:00
string_go120.go bencode: Only use unsafe.String for go>=1.20 2023-04-03 15:10:38 +10:00
tags.go Rework bencode decoding so it might support embedded structs 2021-06-07 13:01:40 +10:00

README.md

Bencode encoding/decoding sub package. Uses similar API design to Go's json package.

Install

go get github.com/anacrolix/torrent

Usage

package demo

import (
	bencode "github.com/anacrolix/torrent/bencode"
)

type Message struct {
	Query    string `json:"q,omitempty" bencode:"q,omitempty"`
}

var v Message

func main(){
	// encode
	data, err := bencode.Marshal(v)
	if err != nil {
		log.Fatal(err)
	}
	
	//decode
	err := bencode.Unmarshal(data, &v)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(v)
}