The Nil Interface Gotcha in Go (Non-Nil Interface Containing Nil Value)

An interface is non-nil if it holds a type, even if that type's value is nil; check the underlying value with reflection or explicit type assertions.

An interface is non-nil if it holds a concrete type, even if that type's value is nil. Use reflect.ValueOf(i).IsNil() to check the underlying value, or explicitly compare the concrete type to nil.

var i interface{} = (*MyType)(nil)
if i != nil {
    // i is non-nil, but the underlying *MyType is nil
    if reflect.ValueOf(i).IsNil() {
        fmt.Println("Interface is non-nil, but value is nil")
    }
}