go type_error ai_generated true

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

ID: go/protobuf-reflection-nil-descriptor

Also available as: JSON · Markdown · 中文
80%Fix Rate
85%Confidence
0Evidence
2025-03-08First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
google.golang.org/protobuf 1.28+ active — — —

Root Cause

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

中文

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

Workarounds

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

Dead Ends

Common approaches that don't work:

  1. 85% fail

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

  2. 70% fail

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