# proto：必填字段 "user.id" 未设置

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

## 根因

proto2 消息中存在必填字段，在序列化前未赋值。proto3 已移除 required，但 proto2（及部分迁移中的 schema）仍强制校验。

## 版本兼容性

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

## 解决方案

1. **** (90% 成功率)
   ```
   Populate required fields before marshal, or migrate the schema to proto3:
msg := &pb.User{Id: userID, Name: name}
if err := proto.Marshal(msg); err != nil { return err }
   ```
2. **** (85% 成功率)
   ```
   If the field truly is optional, edit the .proto to remove `required` and regenerate:
// user.proto
message User { optional string id = 1; }
   ```

## 无效尝试

- **** — Produces wire bytes that downstream strict servers reject; the field is still missing. (60% 失败率)
- **** — proto2 distinguishes 'unset' from 'zero'; setting zero still counts as set only if you explicitly assign it — but semantically you're sending bad data. (75% 失败率)
