Thanks to visit codestin.com
Credit goes to Github.com

Skip to content

πŸš€ Zero-reflection binary protocol code generator for Go - automatically generates fast and type-safe encode/decode methods for structs

License

Notifications You must be signed in to change notification settings

Nyarum/bytespack

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

24 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

BytesPack

Go Version License

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.

Features

  • πŸš€ 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

Installation

go install github.com/Nyarum/bytespack/cmd/diho_bytes_generate@latest

Quick Start

  1. Define your struct with the go:generate directive:
//go:generate diho_bytes_generate packet.go
type Packet struct {
    ID     uint16
    Name   string
    Level  uint32
    Health uint8
}
  1. Run the code generation:
go generate ./...

This will create two files:

  • packet_encode.gen.go: Contains the encoding logic
  • packet_decode.gen.go: Contains the decoding logic

Advanced Usage

Struct Tags

BytesPack supports custom behavior through struct tags:

  • dbg:"ignore" - Skip this field during encoding/decoding
  • dbg:"little" - Use little-endian encoding for this field
  • dbg:"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
}

Custom Filtering

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
}

Project Structure

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

Performance

BytesPack generates code that:

  • Avoids reflection completely
  • Uses efficient byte buffer pooling
  • Minimizes allocations
  • Provides predictable performance

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

About

πŸš€ Zero-reflection binary protocol code generator for Go - automatically generates fast and type-safe encode/decode methods for structs

Resources

License

Stars

Watchers

Forks

Packages

No packages published