|
1 | | -# bencode |
2 | | -Bencode implementation in golang |
| 1 | +# Bencode |
| 2 | + |
| 3 | +[](https://github.com/extintor/bencode/actions/workflows/build.yml) |
| 4 | +[](https://github.com/extintor/bencode/actions/workflows/lint.yml) |
| 5 | + |
| 6 | +## Overview |
| 7 | + |
| 8 | +This Bencode implementation in Go is distinct in its approach to encoding and decoding data. It utilizes Go struct tags, allowing developers to define objects for serialization and deserialization in a manner akin to JSON. This feature enhances the ease of use and integration with Go's native data structures, providing a seamless and efficient way to work with Bencode data. |
| 9 | + |
| 10 | +## Features |
| 11 | + |
| 12 | +- Struct Tag-Based Serialization: Encode and decode Bencode data using Go struct tags, offering a familiar and intuitive approach similar to JSON serialization. |
| 13 | +- Support for Complex Types: Effortlessly handle complex data structures, including nested structs and slices, ensuring versatility in data representation. |
| 14 | +- Streamlined Integration: Designed to align naturally with Go's standard practices, making it easy to incorporate into Go projects. |
| 15 | +- Full Bencode Type Support: Efficiently manage all standard Bencode types like strings, integers, lists, and dictionaries. |
| 16 | + |
| 17 | +## Getting Started |
| 18 | +### Prerequisites |
| 19 | + |
| 20 | +Go (Version 1.21 or higher) |
| 21 | + |
| 22 | +### Installation |
| 23 | + |
| 24 | +```bash |
| 25 | +go get github.com/extintor/bencode |
| 26 | +``` |
| 27 | + |
| 28 | +### Usage |
| 29 | +#### Decoding |
| 30 | + |
| 31 | +```Go |
| 32 | +package main |
| 33 | + |
| 34 | +import ( |
| 35 | + "fmt" |
| 36 | + "github.com/extintor/bencode" |
| 37 | +) |
| 38 | + |
| 39 | +func main() { |
| 40 | + type Profile struct { |
| 41 | + Name string `bencode:"name"` |
| 42 | + Age uint64 `bencode:"age"` |
| 43 | + Hobbies []string `bencode:"hobbies"` |
| 44 | + } |
| 45 | + |
| 46 | + type User struct { |
| 47 | + Username string `bencode:"username"` |
| 48 | + Email string `bencode:"email"` |
| 49 | + Profile Profile `bencode:"profile"` |
| 50 | + } |
| 51 | + |
| 52 | + input := []byte("d8:username8:johndoe5:email15:john@example.com7:profiled4:name9:John Doe3:agei30e7:hobbiesl7:guitar6:travel5:cookingeee") |
| 53 | + var user User |
| 54 | + |
| 55 | + err := bencode.Decode(input, &user) |
| 56 | + if err != nil { |
| 57 | + panic(err) |
| 58 | + } |
| 59 | + |
| 60 | + fmt.Printf("Decoded User: %+v\n", user) |
| 61 | +} |
| 62 | +``` |
| 63 | + |
| 64 | +#### Encoding |
| 65 | + |
| 66 | +```Go |
| 67 | +package main |
| 68 | + |
| 69 | +import ( |
| 70 | + "fmt" |
| 71 | + "github.com/extintor/bencode" |
| 72 | +) |
| 73 | + |
| 74 | +func main() { |
| 75 | + type Profile struct { |
| 76 | + Name string `bencode:"name"` |
| 77 | + Age uint64 `bencode:"age"` |
| 78 | + Hobbies []string `bencode:"hobbies"` |
| 79 | + } |
| 80 | + |
| 81 | + type User struct { |
| 82 | + Username string `bencode:"username"` |
| 83 | + Email string `bencode:"email"` |
| 84 | + Profile Profile `bencode:"profile"` |
| 85 | + } |
| 86 | + |
| 87 | + user := User{ |
| 88 | + Username: "johndoe", |
| 89 | + Email: "john@example.com", |
| 90 | + Profile: Profile{ |
| 91 | + Name: "John Doe", |
| 92 | + Age: 30, |
| 93 | + Hobbies: []string{"guitar", "travel", "cooking"}, |
| 94 | + }, |
| 95 | + } |
| 96 | + |
| 97 | + encodedData, err := bencode.Encode(&user) |
| 98 | + if err != nil { |
| 99 | + panic(err) |
| 100 | + } |
| 101 | + |
| 102 | + fmt.Printf("Encoded Data: %s\n", encodedData) |
| 103 | +} |
| 104 | +``` |
| 105 | + |
| 106 | +## Contributing |
| 107 | + |
| 108 | +Contributions are welcome! |
| 109 | + |
| 110 | +## License |
| 111 | + |
| 112 | +This project is licensed under the MIT License - see the LICENSE file for details. |
0 commit comments