go
type_error
ai_generated
true
proto: Marshal called with nil message
ID: go/protobuf-marshal-nil-message
80%Fix Rate
87%Confidence
0Evidence
2024-05-02First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| google.golang.org/protobuf v1.26+ | active | — | — | — |
Root Cause
A typed nil proto message (e.g. (*pb.User)(nil)) was passed to proto.Marshal, or a typed-nil interface was stored and later marshaled.
generic中文
向 proto.Marshal 传入了类型化的 nil proto 消息(如 (*pb.User)(nil)),或将类型化 nil 接口存储后进行了序列化。
Workarounds
-
95% success
if msg == nil { return errors.New("message is nil") } b, err := proto.Marshal(msg) if err != nil { return err } -
90% success
func build() (*pb.User, error) { return &pb.User{Name: "a"}, nil // never return (*pb.User)(nil), nil }
Dead Ends
Common approaches that don't work:
-
85% fail
proto.Marshal returns an error rather than panicking; recover does not catch returned errors.
-
80% fail
The legacy package delegates to the new one and exhibits the same behavior.