go type_error ai_generated true

panic: proto: 消息 pb.User 未实现 protoreflect.ProtoMessage

panic: proto: message pb.User does not implement protoreflect.ProtoMessage

ID: go/protobuf-reflection-nil-descriptor

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

版本兼容性

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

根因分析

传给 proto.Marshal 的结构体不是由 protoc-gen-go 生成的(或由旧版 github.com/golang/protobuf 插件生成)。它缺少新 API 所需的 ProtoReflect() 方法。

English

A struct is being passed to proto.Marshal that was not generated by protoc-gen-go (or was generated by the old github.com/golang/protobuf plugin). It lacks the ProtoReflect() method required by the new API.

generic

解决方案

  1. 95% 成功率
    go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
    protoc --go_out=. --go_opt=paths=source_relative user.proto
  2. 90% 成功率
    // Remove github.com/golang/protobuf imports; use google.golang.org/protobuf/proto
    import "google.golang.org/protobuf/proto"
    b, err := proto.Marshal(&pb.User{...})

无效尝试

常见但无效的做法:

  1. 85% 失败

    Returns a nil descriptor; reflection and marshaling still fail at runtime.

  2. 70% 失败

    Produces JSON, not protobuf wire format; peers expecting protobuf cannot decode.