# proto: 必填字段 "pb.User.id" 未设置

- **ID:** `go/protobuf-required-field-missing`
- **领域:** go
- **类别:** data_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

定义了 proto2 `required` 字段的消息在序列化或反序列化时未设置必填字段。proto3 没有必填字段，因此这只发生在 proto2 或 gogo/protobuf 中。

## 版本兼容性

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

## 解决方案

1. **** (90% 成功率)
   ```
   id := int64(42)
msg := &pb.User{Id: &id}
if err := proto.Marshal(msg); err != nil { return err }
   ```
2. **** (85% 成功率)
   ```
   // user.proto (proto3)
message User {
  optional int64 id = 1;
}
   ```

## 无效尝试

- **** — proto2 treats zero value as unset for scalar required fields; still fails. (80% 失败率)
- **** — Breaking change for existing wire consumers; migration is non-trivial. (50% 失败率)
