Skip to content

Commit d50b167

Browse files
Added go struct tag explanation content.
1 parent 36c81e0 commit d50b167

File tree

2 files changed

+186
-4
lines changed

2 files changed

+186
-4
lines changed

README.md

Lines changed: 93 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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

README_CN.md

Lines changed: 93 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,104 @@ type Example struct {
7878
}
7979
```
8080

81-
### 3. 自动大小追踪
81+
### 3. 结构体标签参考
82+
83+
`struc` 标签支持多种格式和选项,用于精确控制二进制数据:
84+
85+
#### 基本类型定义
86+
87+
```go
88+
type BasicTypes struct {
89+
Int8Val int `struc:"int8"` // 8位整数
90+
Int16Val int `struc:"int16"` // 16位整数
91+
Int32Val int `struc:"int32"` // 32位整数
92+
Int64Val int `struc:"int64"` // 64位整数
93+
UInt8Val int `struc:"uint8"` // 8位无符号整数
94+
UInt16Val int `struc:"uint16"` // 16位无符号整数
95+
UInt32Val int `struc:"uint32"` // 32位无符号整数
96+
UInt64Val int `struc:"uint64"` // 64位无符号整数
97+
BoolVal bool `struc:"bool"` // 布尔值
98+
Float32Val float32 `struc:"float32"` // 32位浮点数
99+
Float64Val float64 `struc:"float64"` // 64位浮点数
100+
}
101+
```
102+
103+
#### 数组和固定大小字段
104+
105+
```go
106+
type ArrayTypes struct {
107+
// 固定大小字节数组(4字节)
108+
ByteArray []byte `struc:"[4]byte"`
109+
// 固定大小整数数组(5个int32值)
110+
IntArray []int32 `struc:"[5]int32"`
111+
// 用于对齐的填充字节
112+
Padding []byte `struc:"[3]pad"`
113+
// 固定大小字符串(作为字节数组处理)
114+
FixedString string `struc:"[8]byte"`
115+
}
116+
```
117+
118+
#### 动态大小和引用
119+
120+
```go
121+
type DynamicTypes struct {
122+
// 追踪 Data 长度的大小字段
123+
Size int `struc:"int32,sizeof=Data"`
124+
// 大小由 Size 追踪的动态字节切片
125+
Data []byte
126+
// 使用 uint8 追踪 AnotherData 的大小字段
127+
Size2 int `struc:"uint8,sizeof=AnotherData"`
128+
// 另一个动态数据字段
129+
AnotherData []byte
130+
// 带大小引用的动态字符串字段
131+
StrSize int `struc:"uint16,sizeof=Text"`
132+
Text string `struc:"[]byte"`
133+
}
134+
```
135+
136+
#### 字节序控制
137+
138+
```go
139+
type ByteOrderTypes struct {
140+
// 大端编码整数
141+
BigInt int32 `struc:"big"`
142+
// 小端编码整数
143+
LittleInt int32 `struc:"little"`
144+
// 未指定则默认为大端
145+
DefaultInt int32
146+
}
147+
```
148+
149+
#### 特殊选项
150+
151+
```go
152+
type SpecialTypes struct {
153+
// 在打包/解包时跳过此字段
154+
Ignored int `struc:"skip"`
155+
// 从其他字段获取大小引用
156+
Data []byte `struc:"sizefrom=Size"`
157+
// 自定义类型实现
158+
Custom Custom
159+
}
160+
```
161+
162+
标签格式:`struc:"type,option1,option2"`
163+
164+
- `type`:二进制类型(如 int8、uint16、[4]byte)
165+
- `big`/`little`:字节序指定
166+
- `sizeof=Field`:指定此字段追踪另一个字段的大小
167+
- `sizefrom=Field`:指定此字段的大小由另一个字段追踪
168+
- `skip`:在打包/解包时跳过此字段
169+
- `[N]type`:长度为 N 的固定大小类型数组
170+
- `[]type`:动态大小的类型数组/切片
171+
172+
### 4. 自动大小追踪
82173

83174
- 自动管理可变大小字段的长度
84175
- 消除手动大小计算和追踪
85176
- 减少二进制协议实现中的潜在错误
86177

87-
### 4. 性能优化
178+
### 5. 性能优化
88179

89180
- 反射缓存以提高重复操作性能
90181
- 高效的内存分配

0 commit comments

Comments
 (0)