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

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

## 根因

传给 proto.Unmarshal 的字节切片不是目标消息的有效 protobuf 编码——通常是客户端与服务端使用了不同的 .proto 定义，或者数据根本不是 protobuf。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| google.golang.org/protobuf v1.28+ | active | — | — |

## 解决方案

1. **** (90% 成功率)
   ```
   Verify the sender and receiver share the exact same .proto file. Regenerate stubs and compare descriptor hashes:
import "google.golang.org/protobuf/reflect/protoreflect"
desc := (&pb.MyMsg{}).ProtoReflect().Descriptor()
log.Printf("msg=%s pkg=%s", desc.FullName(), desc.ParentFile().Package())
   ```
2. **** (80% 成功率)
   ```
   Wrap Unmarshal with a length check and hex dump on failure to identify the payload:
if err := proto.Unmarshal(data, msg); err != nil {
  log.Printf("unmarshal failed: %v, first16=%x", err, data[:min(16,len(data))])
  return err
}
   ```

## 无效尝试

- **** — Wire-format mismatch is a schema problem, not a size problem; retrying with more memory won't help. (95% 失败率)
- **** — If the wire bytes are actually protobuf, JSON parsing fails; if they're JSON, the sender is misconfigured. (85% 失败率)
