Giter VIP home page Giter VIP logo

bencode's Introduction

bencode

build-img pkg-img version-img

Package implements Bencode encoding and decoding in Go.

Features

  • Simple API.
  • Clean and tested code.
  • Optimized for speed.
  • Dependency-free.

See docs.

Install

Go version 1.18+

go get github.com/cristalhq/bencode

Example

Marshaling into Bencode

// data to process, most of the types are supported
var data any = map[string]any{
    "1":     42,
    "hello": "world",
    "foo":   []string{"bar", "baz"},
}

buf, err := bencode.Marshal(data)
checkErr(err)
fmt.Printf("marshaled: %s\n", string(buf))

// or via Encoder:
w := &bytes.Buffer{} // or any other io.Writer
err = bencode.NewEncoder(w).Encode(data)
checkErr(err)

// Output:
// marshaled: d1:1i42e3:fool3:bar3:baze5:hello5:worlde

Unmarshaling from Bencode

var data any

buf := []byte("li1ei42ee")

err := bencode.Unmarshal(buf, &data)
checkErr(err)

// or via Decoder:
r := bytes.NewBufferString("li1ei42ee") // or any other io.Reader
err = bencode.NewDecoder(r).Decode(&data)
checkErr(err)

fmt.Printf("unmarshaled: %v\n", data)

// Output:
// unmarshaled: [1 42]

See examples: example_test.go.

License

MIT License.

bencode's People

Contributors

cristaloleg avatar dependabot[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

s0x90

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.