BytesPack is a powerful Go code generation tool that automatically generates encode/decode methods for your structs to handle binary protocols without using reflection. This makes binary protocol handling both faster and safer.
- π Zero reflection for better performance
- π Automatic code generation for encode/decode methods
- πͺ Support for various Go types including:
- Basic types (uint8/16/32/64, int8/16/32/64)
- Strings with null termination
- Byte slices
- Arrays and slices
- Nested structs
- π§ Custom endianness support via struct tags
- π― Field filtering capabilities
- π¦ Uses efficient byte buffer pool for better memory management
go install github.com/Nyarum/bytespack/cmd/diho_bytes_generate@latest- Define your struct with the
go:generatedirective:
//go:generate diho_bytes_generate packet.go
type Packet struct {
ID uint16
Name string
Level uint32
Health uint8
}- Run the code generation:
go generate ./...This will create two files:
packet_encode.gen.go: Contains the encoding logicpacket_decode.gen.go: Contains the decoding logic
BytesPack supports custom behavior through struct tags:
dbg:"ignore"- Skip this field during encoding/decodingdbg:"little"- Use little-endian encoding for this fielddbg:"fieldName==value"- Conditional encoding/decoding based on other field values
Example:
type Packet struct {
Header `dbg:"ignore,little"`
ID uint16
OptionalField uint32 `dbg:"ID==1"` // Only encoded/decoded if ID equals 1
}You can implement custom filtering logic by adding a Filter method to your struct:
func (p *Packet) Filter(ctx context.Context, fieldName string) bool {
// Return true to skip encoding/decoding of the current field
return false
}bytespack/
βββ cmd/
β βββ diho_bytes_generate/ # Code generation tool
βββ customtypes/ # Custom type definitions
βββ example/ # Usage examples
βββ generate/ # Code generation logic
βββ parse/ # Struct parsing logic
βββ utils/ # Utility functions
BytesPack generates code that:
- Avoids reflection completely
- Uses efficient byte buffer pooling
- Minimizes allocations
- Provides predictable performance
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.