go
data_error
ai_generated
true
proto: 必填字段 "User.email" 未设置
proto: required field "User.email" not set
ID: go/proto-required-field-not-set
80%修复率
86%置信度
0证据数
2024-02-28首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| github.com/golang/protobuf v1.5 / proto2 | active | — | — | — |
根因分析
proto2 的 required 字段在序列化前必须显式设置。proto3 没有 required 字段,因此该错误只出现在 proto2 或旧版生成代码中。
English
proto2 required fields must be explicitly set before marshaling. In proto3 there are no required fields, so this only appears with proto2 or legacy generated code.
解决方案
-
90% 成功率
u.Email = proto.String("[email protected]") if err := proto.Marshal(u); err != nil { log.Fatalf("marshal: %v", err) } -
80% 成功率
// user.proto: syntax = "proto3"; // string email = 1 [ (validate.rules).string.email = true ];
无效尝试
常见但无效的做法:
-
95% 失败
Marshaling is deterministic; an unset required field stays unset. Retrying never fills the field.
-
50% 失败
Regenerating changes the API; callers relying on presence semantics may now send empty values silently.