# panic: proto: Marshal called with nil

- **ID:** `go/proto-nil-message-fields`
- **Domain:** go
- **Category:** runtime_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## 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.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| google.golang.org/protobuf 1.28+ | active | — | — |

## Workarounds

1. **** (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 }
   ```
2. **** (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

- **** — The nil message indicates a logic bug; swallowing the panic leaves a corrupt state and the message is never sent, causing silent data loss. (85% fail)
- **** — Marshalling an empty struct produces an empty byte slice, which downstream code interprets as a valid but empty message, corrupting semantics. (75% fail)
