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

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

## 根因

访问从未设置的 proto 消息的子消息字段会返回 nil。调用 getter 是安全的，但对 nil 子消息直接进行字段访问会 panic。

## 版本兼容性

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

## 解决方案

1. **** (95% 成功率)
   ```
   // BAD: msg.Address.City
// GOOD:
city := msg.GetAddress().GetCity()
if city == "" { city = "unknown" }
   ```
2. **** (93% 成功率)
   ```
   if addr := msg.GetAddress(); addr != nil {
    fmt.Println(addr.Street)
} else {
    fmt.Println("address not set")
}
   ```

## 无效尝试

- **** — recover hides the bug and leaves the RPC handler in an inconsistent state; the underlying nil field is still unset. (80% 失败率)
- **** — The peer or the server-side merge may still leave optional submessages unset, so the panic returns whenever the message is absent. (65% 失败率)
