Documentation
¶
Index ¶
- func AppendBytes(b []byte, s []byte) []byte
- func AppendInt(b []byte, i int64) []byte
- func AppendStr(b []byte, s string) []byte
- func Marshal(v any) ([]byte, error)
- func Unmarshal(data []byte, v any) error
- func UnmarshalRelaxed(data []byte, v any) error
- type Encoder
- type IsZeroValue
- type Marshaler
- type RawBytes
- type Unmarshaler
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AppendBytes ¶ added in v0.0.13
func Marshal ¶
Example ¶
package main
import (
"fmt"
"github.com/trim21/go-bencode"
)
func main() {
type User struct {
ID uint32 `bencode:"id,string"`
Name string `bencode:"name"`
}
type Inner struct {
V int `bencode:"v"`
S string `bencode:"a long string name replace field name"`
}
type With struct {
Users []User `bencode:"users,omitempty"`
Obj Inner `bencode:"obj"`
Ignored bool `bencode:"-"`
}
var data = With{
Users: []User{
{ID: 1, Name: "sai"},
{ID: 2, Name: "trim21"},
},
Obj: Inner{V: 2, S: "vvv"},
}
var b, err = bencode.Marshal(data)
if err != nil {
panic(err)
}
fmt.Println(string(b))
}
Output: d3:objd37:a long string name replace field name3:vvv1:vi2ee5:usersld2:idi1e4:name3:saied2:idi2e4:name6:trim21eee
func Unmarshal ¶
Example ¶
package main
import (
"fmt"
"github.com/trim21/go-bencode"
)
func main() {
var v struct {
S map[[20]byte]struct{} `bencode:"s"`
Value map[string]string `bencode:"value" json:"value"`
}
raw := `d5:valued4:five1:5ee`
err := bencode.Unmarshal([]byte(raw), &v)
if err != nil {
panic(err)
}
fmt.Println(v.Value["five"])
}
Output: 5
func UnmarshalRelaxed ¶ added in v0.1.0
UnmarshalRelaxed is like Unmarshal but with relaxed parsing rules: - Dictionary keys are not required to be sorted - Duplicate dictionary keys are allowed (last value wins)
Types ¶
type Encoder ¶ added in v0.0.8
type Encoder struct {
// contains filtered or unexported fields
}
func NewEncoder ¶ added in v0.0.9
type IsZeroValue ¶ added in v0.0.4
type IsZeroValue interface {
// IsZeroBencodeValue enable support for omitempty feature.
// if it's being used as struct field, and it returns true, this field will be skipped.
IsZeroBencodeValue() bool
}
IsZeroValue add support type implements Marshaler and omitempty
var s struct {
Field T `bencode:"field,omitempty"`
}
if `T` implements Marshaler, it can implement IsZeroValue so bencode know if it's
type RawBytes ¶ added in v0.0.3
type RawBytes []byte
func (RawBytes) IsZeroBencodeValue ¶ added in v0.0.4
func (RawBytes) MarshalBencode ¶ added in v0.0.3
func (*RawBytes) UnmarshalBencode ¶ added in v0.0.3
type Unmarshaler ¶
Click to show internal directories.
Click to hide internal directories.