What Is a Nil Interface vs a Nil Concrete Value in Go

A nil interface holds no type or value, while a nil concrete value is a specific type that is nil but still makes the interface non-nil.

A nil interface value is an interface variable that holds no type or value, whereas a nil concrete value is a specific type (like a pointer or slice) that is nil but still stored inside a non-nil interface. You can distinguish them because a nil concrete value is not equal to a nil interface, even if the underlying data is nil. Use reflect.TypeOf() to check if the interface holds a type; if it returns nil, the interface itself is nil, but if it returns a type, the interface is non-nil even if the value inside is nil.

var i interface{}
var p *int = nil
i = p

// i is not nil because it holds the type *int, even though p is nil
if i == nil {
    fmt.Println("i is nil")
} else {
    fmt.Println("i is not nil, but holds a nil value")
}

// Check the concrete type
if reflect.TypeOf(i) == nil {
    fmt.Println("Interface is nil")
} else {
    fmt.Println("Interface holds a type:", reflect.TypeOf(i))
}