# panic: 运行时错误：proto.Unmarshal 中无效的内存地址或 nil 指针解引用

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

## 根因

调用 proto.Unmarshal 时传入了 nil 消息指针，或生成消息的内部字段未初始化（例如使用零值结构体字面量而非构造函数）。

## 版本兼容性

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

## 解决方案

1. **** (95% 成功率)
   ```
   msg := &pb.User{}
if err := proto.Unmarshal(data, msg); err != nil { return err }
   ```
2. **** (90% 成功率)
   ```
   msg := pb.NewUser() // or &pb.User{} then reset
if err := proto.Unmarshal(data, msg); err != nil { return err }
   ```

## 无效尝试

- **** — Swallowing the panic leaves the message nil; subsequent field access panics again and corrupts control flow. (85% 失败率)
- **** — proto.Unmarshal requires a proto.Message (pointer receiver); passing a value fails to compile or operates on a copy. (80% 失败率)
