# proto: Marshal 被传入 nil 消息

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

## 根因

向 proto.Marshal 传入了类型化的 nil proto 消息（如 (*pb.User)(nil)），或将类型化 nil 接口存储后进行了序列化。

## 版本兼容性

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

## 解决方案

1. **** (95% 成功率)
   ```
   if msg == nil {
    return errors.New("message is nil")
}
b, err := proto.Marshal(msg)
if err != nil { return err }
   ```
2. **** (90% 成功率)
   ```
   func build() (*pb.User, error) {
    return &pb.User{Name: "a"}, nil // never return (*pb.User)(nil), nil
}
   ```

## 无效尝试

- **** — proto.Marshal returns an error rather than panicking; recover does not catch returned errors. (85% 失败率)
- **** — The legacy package delegates to the new one and exhibits the same behavior. (80% 失败率)
