# panic：运行时错误：无效的内存地址或空指针解引用

- **ID:** `go/proto-nil-message-panic`
- **领域:** go
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

向 proto.Marshal、proto.Clone 传递了 nil *Message 指针，或在 nil 接收者上调用了会解引用 map 字段的生成 getter。

## 版本兼容性

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

## 解决方案

1. **** (95% 成功率)
   ```
   if msg == nil {
    return nil, status.Error(codes.InvalidArgument, "nil message")
}
data, err := proto.Marshal(msg)
if err != nil { return nil, err }
   ```
2. **** (85% 成功率)
   ```
   clone := proto.Clone(msg) // returns nil if msg is nil
if clone == nil {
    return nil, errors.New("nil proto message")
}
   ```

## 无效尝试

- **** — Masks the nil bug; the same nil pointer will surface elsewhere or cause silent data loss. (85% 失败率)
- **** — AllowPartial only affects required-field validation, not nil pointer dereferences. (95% 失败率)
