@@ -78,13 +78,104 @@ type Example struct {
7878}
7979```
8080
81- ### 3. Automatic Size Tracking
81+ ### 3. Struct Tag Reference
82+
83+ The ` struc ` tag supports various formats and options for precise binary data control:
84+
85+ #### Basic Type Definition
86+
87+ ``` go
88+ type BasicTypes struct {
89+ Int8Val int ` struc:"int8"` // 8-bit integer
90+ Int16Val int ` struc:"int16"` // 16-bit integer
91+ Int32Val int ` struc:"int32"` // 32-bit integer
92+ Int64Val int ` struc:"int64"` // 64-bit integer
93+ UInt8Val int ` struc:"uint8"` // 8-bit unsigned integer
94+ UInt16Val int ` struc:"uint16"` // 16-bit unsigned integer
95+ UInt32Val int ` struc:"uint32"` // 32-bit unsigned integer
96+ UInt64Val int ` struc:"uint64"` // 64-bit unsigned integer
97+ BoolVal bool ` struc:"bool"` // Boolean value
98+ Float32Val float32 ` struc:"float32"` // 32-bit float
99+ Float64Val float64 ` struc:"float64"` // 64-bit float
100+ }
101+ ```
102+
103+ #### Array and Fixed-Size Fields
104+
105+ ``` go
106+ type ArrayTypes struct {
107+ // Fixed-size byte array (4 bytes)
108+ ByteArray []byte ` struc:"[4]byte"`
109+ // Fixed-size integer array (5 int32 values)
110+ IntArray []int32 ` struc:"[5]int32"`
111+ // Padding bytes for alignment
112+ Padding []byte ` struc:"[3]pad"`
113+ // Fixed-size string (treated as byte array)
114+ FixedString string ` struc:"[8]byte"`
115+ }
116+ ```
117+
118+ #### Dynamic Size and References
119+
120+ ``` go
121+ type DynamicTypes struct {
122+ // Size field tracking the length of Data
123+ Size int ` struc:"int32,sizeof=Data"`
124+ // Dynamic byte slice whose size is tracked by Size
125+ Data []byte
126+ // Size field using uint8 to track AnotherData
127+ Size2 int ` struc:"uint8,sizeof=AnotherData"`
128+ // Another dynamic data field
129+ AnotherData []byte
130+ // Dynamic string field with size reference
131+ StrSize int ` struc:"uint16,sizeof=Text"`
132+ Text string ` struc:"[]byte"`
133+ }
134+ ```
135+
136+ #### Byte Order Control
137+
138+ ``` go
139+ type ByteOrderTypes struct {
140+ // Big-endian encoded integer
141+ BigInt int32 ` struc:"big"`
142+ // Little-endian encoded integer
143+ LittleInt int32 ` struc:"little"`
144+ // Default to big-endian if not specified
145+ DefaultInt int32
146+ }
147+ ```
148+
149+ #### Special Options
150+
151+ ``` go
152+ type SpecialTypes struct {
153+ // Skip this field during packing/unpacking
154+ Ignored int ` struc:"skip"`
155+ // Size reference from another field
156+ Data []byte ` struc:"sizefrom=Size"`
157+ // Custom type implementation
158+ Custom Custom
159+ }
160+ ```
161+
162+ Tag Format: ` struc:"type,option1,option2" `
163+
164+ - ` type ` : The binary type (e.g., int8, uint16, [ 4] byte)
165+ - ` big ` /` little ` : Byte order specification
166+ - ` sizeof=Field ` : Specify this field tracks another field's size
167+ - ` sizefrom=Field ` : Specify this field's size is tracked by another field
168+ - ` skip ` : Skip this field during packing/unpacking
169+ - ` [N]type ` : Fixed-size array of type with length N
170+ - ` []type ` : Dynamic-size array/slice of type
171+
172+ ### 4. Automatic Size Tracking
82173
83174- Automatically manages lengths of variable-sized fields
84175- Eliminates manual size calculation and tracking
85176- Reduces potential errors in binary protocol implementations
86177
87- ### 4 . Performance Optimizations
178+ ### 5 . Performance Optimizations
88179
89180- Reflection caching for repeated operations
90181- Efficient memory allocation
0 commit comments