# panic: proto: 消息为 nil

- **ID:** `go/protobuf-nil-pointer-unmarshal`
- **领域:** go
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

proto.Unmarshal 或 proto.Marshal 被传入了 nil 消息指针，例如 var m *pb.User; proto.Unmarshal(data, m)。生成的方法解引用该指针并 panic。

## 版本兼容性

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

## 解决方案

1. **** (95% 成功率)
   ```
   Always allocate with m := &pb.User{} or m := new(pb.User) before Unmarshal: proto.Unmarshal(data, m).
   ```
2. **** (88% 成功率)
   ```
   Use proto.Clone or a constructor helper that guarantees a non-nil pointer, and add a staticcheck/errcheck rule to catch nil pointer args.
   ```

## 无效尝试

- **** — Recovering hides a programming bug and returns a zero-value message that downstream logic treats as valid data. (80% 失败率)
- **** — The panic occurs on the call itself, before any error is returned, so the check never runs. (85% 失败率)
