# panic：proto：Marshal 被以 nil 调用

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

## 根因

proto.Marshal 接收到 nil 指针，通常源于处理函数返回 (nil, nil) 或未初始化的嵌套字段。

## 版本兼容性

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

## 解决方案

1. **** (95% 成功率)
   ```
   Always return a non-nil message or an explicit status error:
if user == nil {
  return nil, status.Error(codes.NotFound, "user not found")
}
return &pb.GetUserResponse{User: user}, nil
   ```
2. **** (90% 成功率)
   ```
   Add a unit test that asserts every handler returns either a non-nil message or a non-nil error.
   ```

## 无效尝试

- **** — Masks the bug; nil responses keep leaking to clients as empty messages. (85% 失败率)
- **** — Changes the API contract; clients can no longer distinguish 'no result' from 'empty result'. (70% 失败率)
