# proto: 无法解析无效的 wire-format 数据

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

## 根因

传给 proto.Unmarshal 的字节不符合 protobuf wire format——通常因为数据是 JSON、空、被截断，或完全是另一种 proto 消息类型。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| google.golang.org/protobuf 1.x | active | — | — |

## 解决方案

1. **** (85% 成功率)
   ```
   var msg pb.User
if json.Valid(data) {
  err = protojson.Unmarshal(data, &msg)
} else {
  err = proto.Unmarshal(data, &msg)
}
if err != nil { return fmt.Errorf("decode: %w", err) }
   ```
2. **** (80% 成功率)
   ```
   if len(data) > 0 {
  log.Printf("first bytes: %x", data[:min(len(data),16)])
}
// JSON starts with '{' = 0x7b; protobuf field tag usually 0x0a etc.
   ```

## 无效尝试

- **** — Wire format is deterministic; identical bytes yield identical failure. (95% 失败率)
- **** — If bytes are binary protobuf, JSON parser fails; if JSON, the original was wrong type. (70% 失败率)
