# 错误：proto：无法序列化，消息为 nil

- **ID:** `go/grpc-proto-marshal-error`
- **领域:** go
- **类别:** data_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

尝试将 nil protobuf 消息序列化为字节，这在 protobuf 中是不允许的。

## 版本兼容性

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

## 解决方案

1. **Initialize the message before marshaling** (99% 成功率)
   ```
   msg := &MyMessage{}
b, err := proto.Marshal(msg)
   ```
2. **Use proto.MarshalOptions with deterministic output** (98% 成功率)
   ```
   b, err := proto.MarshalOptions{}.Marshal(msg)
   ```

## 无效尝试

- **Use a zero-value message instead of nil** — A zero-value message still marshals to empty bytes, but if the message is nil, it will panic or error. (50% 失败率)
- **Check for nil but continue with empty bytes** — You need to handle the nil case explicitly, otherwise the error propagates. (40% 失败率)
