go build_error ai_generated true

panic: proto: 消息 pb.User 中字段编号 5 重复

panic: proto: duplicate field number 5 in message pb.User

ID: go/protobuf-duplicate-field-number

其他格式: JSON · Markdown 中文 · English
80%修复率
89%置信度
0证据数
2025-01-09首次发现

版本兼容性

版本状态引入弃用备注
1.x active — — —

根因分析

同一消息中的两个字段共享相同的字段编号,protobuf 禁止此行为。通常由手工编辑生成代码或合并冲突的 .proto 定义引起。

English

Two fields in the same message share a field number, which protobuf forbids. This usually arises from hand-editing generated code or merging conflicting .proto definitions.

generic

解决方案

  1. 95% 成功率
    Assign a unique field number to each field in the .proto, then regenerate with protoc --go_out=. --go-grpc_out=. and never hand-edit generated files.
  2. 90% 成功率
    Use reserved field numbers to prevent reuse: reserved 5; when removing a field, and run buf lint to catch duplicate numbers at build time.

无效尝试

常见但无效的做法:

  1. 95% 失败

    The duplicate numbering corrupts wire format; removing the check produces silent data corruption instead of a clear failure.

  2. 90% 失败

    The number, not the name, is what must be unique; the panic persists.