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

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

## 根因

被反序列化的字节不是有效的 protobuf wire format —— 通常是因为发送了 JSON、gzip 压缩载荷或使用了不同 schema 的消息。

## 版本兼容性

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

## 解决方案

1. **** (90% 成功率)
   ```
   if err := proto.Unmarshal(data, msg); err != nil {
    log.Printf("unmarshal failed: %v, first bytes: % x", err, data[:min(32, len(data))])
    return err
}
   ```
2. **** (86% 成功率)
   ```
   // client
conn, _ := grpc.NewClient(addr,
    grpc.WithDefaultCallOptions(grpc.ForceCodec(proto.Marshaler{})),
)
// server: verify incoming Content-Type header
md, _ := metadata.FromIncomingContext(ctx)
log.Printf("content-type: %v", md["content-type"])
   ```

## 无效尝试

- **** — Unmarshal is deterministic; invalid wire bytes always fail at the same offset. (99% 失败率)
- **** — Doesn't fix the underlying wire corruption; just moves the failure deeper. (85% 失败率)
