# proto: Marshal called with nil message

- **ID:** `go/protobuf-marshal-nil-message`
- **Domain:** go
- **Category:** type_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

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

## Version Compatibility

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

## Workarounds

1. **** (95% success)
   ```
   if msg == nil {
    return errors.New("message is nil")
}
b, err := proto.Marshal(msg)
if err != nil { return err }
   ```
2. **** (90% success)
   ```
   func build() (*pb.User, error) {
    return &pb.User{Name: "a"}, nil // never return (*pb.User)(nil), nil
}
   ```

## Dead Ends

- **** — proto.Marshal returns an error rather than panicking; recover does not catch returned errors. (85% fail)
- **** — The legacy package delegates to the new one and exhibits the same behavior. (80% fail)
