go runtime ai_generated true

interface conversion: interface is nil, not T

ID: go/interface-conversion-panic

Also available as: JSON · Markdown
95%Fix Rate
96%Confidence
50Evidence
2023-01-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
121 active

Root Cause

Type assertion on a nil interface value.

generic

Workarounds

  1. 96% success Use the two-value type assertion: val, ok := iface.(T); if !ok { handle }
    Safe form doesn't panic; ok is false if assertion fails

    Sources: https://go.dev/tour/methods/15

  2. 90% success Check for nil before type-asserting: if iface != nil { val := iface.(T) }
    Nil interfaces always fail type assertions

Dead Ends

Common approaches that don't work:

  1. Using recover() globally to catch the panic 75% fail

    Masks the nil interface bug; inconsistent state continues

  2. Checking for nil after the assertion 85% fail

    Panic already happened; nil check is too late

Error Chain

Frequently confused with: