go build_error ai_generated true

panic:proto:消息 pb.Order 中的字段编号 3 已被 "id" 使用

panic: proto: field number 3 in message pb.Order is already used by "id"

ID: go/proto-duplicate-field-number

其他格式: JSON · Markdown 中文 · English
80%修复率
90%置信度
0证据数
2024-03-19首次发现

版本兼容性

版本状态引入弃用备注
google.golang.org/protobuf 1.31+ active — — —

根因分析

同一个 proto 消息中的两个字段被分配了相同的字段编号。这是 proto 模式错误,在初始化时被生成的代码捕获。

English

Two fields in the same proto message were assigned the same field number. This is a proto schema error caught at init time by the generated code.

generic

解决方案

  1. 97% 成功率
    Assign unique field numbers in the .proto file and regenerate:
    message Order {
      int64 id = 1;
      string sku = 2;
      int32 qty = 3;  // not another 3
    }
    // then: protoc --go_out=. order.proto
  2. 92% 成功率
    Run buf lint in CI to catch duplicate field numbers before build:
    buf lint && buf breaking --against '.git#branch=main'

无效尝试

常见但无效的做法:

  1. 90% 失败

    The panic is triggered by the proto field numbers in the generated descriptors, not by Go field names; renaming Go fields does not change the descriptor.

  2. 95% 失败

    The panic occurs during package init before main runs; recover cannot intercept init-time panics.