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

- **ID:** `go/grpc-unary-interceptor-nil-response`
- **领域:** go
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

一元拦截器返回了 nil 错误但将回复消息留为 nil，或处理函数返回了 nil 响应和 nil 错误。调用方随后解引用该 nil 回复。

## 版本兼容性

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

## 解决方案

1. **** (92% 成功率)
   ```
   Ensure handlers always return a non-nil message with nil error: return &pb.UserResponse{...}, nil; and validate in interceptors that reply != nil before returning.
   ```
2. **** (85% 成功率)
   ```
   Use protoc-gen-validate or manual checks to guarantee non-nil responses, and add a unit test that asserts the handler never returns (nil, nil).
   ```

## 无效尝试

- **** — Recovering hides the nil reply and returns a zero-value to the caller, which then processes garbage. (80% 失败率)
- **** — The panic may occur inside the server handler or interceptor chain before the response is sent. (75% 失败率)
