torrent/bencode
YenForYang a8db640c62
Drop bradfitz/iter dependency (#605)
* Drop bradfitz/iter dependency

`range iter.N` looks nice and doesn't allocate, but unfortunately using a `range` expression blocks a function from being inlined wherever it's used (for now). It's not that we need inlining in all cases, but I do think a C-style for loop looks just as nice and is probably clearer to the majority. There also aren't any clear disadvantages to changing (unless you just happen to dislike the look of C)

* Update misc_test.go

* Update rlreader_test.go

* Update torrent_test.go

* Update bench_test.go

* Update client_test.go

* Update iplist_test.go

* Update mse_test.go

* Update peerconn_test.go

* Update peerconn.go

* Update order_test.go

* Update decoder_test.go

* Update main.go

* Update bench-piece-mark-complete.go

* Update main.go

* Update torrent.go

* Update iplist_test.go

* Update main.go
2021-09-14 13:46:50 +10:00
..
testdata bencode: Use the testdata convention in tests 2016-02-23 21:31:15 +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 Drop bradfitz/iter dependency (#605) 2021-09-14 13:46:50 +10:00
both_test.go bencode: Update some test code 2016-02-23 21:47:00 +11:00
bytes.go Make bencode.Bytes work with pointer and nonpointer receivers 2016-08-26 20:29:29 +10:00
decode.go Add linter CI (#542) 2021-08-16 11:11:31 +10:00
decode_test.go Don't panic on int parse failures 2021-08-12 13:46:02 +10:00
encode.go bencode: Improve support for embedded structs 2021-06-07 13:01:40 +10:00
encode_test.go bencode: Fix marshalling of []byte(nil) 2018-11-27 08:57:17 +11:00
fuzz.go Fix go:build directives 2021-07-14 14:35:52 +10:00
misc.go Cheaper byte to string conversion (#602) 2021-09-14 10:41:04 +10:00
scanner.go bencode.scanner.ReadByte returned errors when it shouldn't have 2017-11-10 23:44:24 +11: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)
}