# 恐慌：运行时错误：无效的内存地址或空指针解引用 [信号SIGSEGV]

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

## 根因

访问protobuf oneof字段而未检查是否已设置，导致空指针解引用。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 1.0 | active | — | — |
| 1.1 | active | — | — |

## 解决方案

1. **Check if oneof field is non-nil before type assertion.** (98% 成功率)
   ```
   if v := msg.GetPayload(); v != nil {
    switch x := v.(type) {
    case *pb.Payload_Text:
        fmt.Println(x.Text)
    }
}
   ```
2. **Use proto.GetField to safely retrieve oneof values.** (85% 成功率)
   ```
   import "google.golang.org/protobuf/proto"
field := proto.GetField(msg, "payload")
   ```

## 无效尝试

- **Use a type switch without nil check.** — Oneof interface can be nil; type switch panics. (95% 失败率)
- **Assume the oneof is always populated.** — Unmarshal may leave it nil if field is absent. (90% 失败率)
