# proto: 无法解析无效的线格式数据：非法标签 0（线类型 0）

- **ID:** `go/protobuf-unmarshal-invalid-wire-type`
- **领域:** go
- **类别:** data_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

protobuf 二进制数据损坏或不是预期的格式。这可能在数据被截断、通过非二进制通道发送或使用错误的消​​息类型进行反序列化时发生。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 1.x | active | — | — |

## 解决方案

1. **** (90% 成功率)
   ```
   // Before unmarshaling, check data length and source
if len(data) == 0 {
    return errors.New("empty data")
}
var msg YourMessage
if err := proto.Unmarshal(data, &msg); err != nil {
    log.Printf("unmarshal failed: %v, data: %x", err, data)
    return err
}
   ```
2. **** (85% 成功率)
   ```
   // If data is JSON, use protojson
var msg YourMessage
if err := protojson.Unmarshal(jsonData, &msg); err != nil {
    log.Fatalf("protojson unmarshal failed: %v", err)
}
   ```

## 无效尝试

- **** — The error is not about buffer size but about invalid data structure; increasing buffer won't fix corrupted data. (95% 失败率)
- **** — This leads to silent data corruption and unpredictable behavior downstream. (90% 失败率)
