go
runtime_error
ai_generated
true
panic: proto: Marshal called with nil
ID: go/proto-nil-message-fields
80%Fix Rate
89%Confidence
0Evidence
2024-01-22First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| google.golang.org/protobuf 1.28+ | active | — | — | — |
Root Cause
proto.Marshal was called with a nil *Message pointer. This happens when an uninitialized message field or a nil response is passed directly to Marshal.
generic中文
调用 proto.Marshal 时传入了 nil 的 *Message 指针。当未初始化的消息字段或 nil 响应直接传给 Marshal 时发生。
Workarounds
-
95% success
Guard against nil before marshalling: if msg == nil { return nil, status.Error(codes.Internal, "nil message") } b, err := proto.Marshal(msg) if err != nil { return nil, err } -
90% success
Initialize nested message fields with proto message constructors instead of leaving them nil: resp := &pb.Response{Data: &pb.Data{}} // not just &pb.Response{}
Dead Ends
Common approaches that don't work:
-
85% fail
The nil message indicates a logic bug; swallowing the panic leaves a corrupt state and the message is never sent, causing silent data loss.
-
75% fail
Marshalling an empty struct produces an empty byte slice, which downstream code interprets as a valid but empty message, corrupting semantics.