3 ms·
It's really not confusing if you understand the type system (yes, yes, I know). A nil pointer is a valid value and can implement methods and satisfy an interfa
by Ferret7446 4y ago
It's really not confusing if you understand the type system (yes, yes, I know).
A nil pointer is a valid value and can implement methods and satisfy an interface. An uninitialized interface cannot.
type foo struct {}
func (*foo) String() string { return "foo" }
type Stringer interface{ String() string }
func main() {
var x Stringer
x.String() // panics
x == nil // This is a nil interface value
x = (*foo)(nil)
x.String() // works
x != nil // This is not a nil interface. It is set to a valid implementation, even if it's implemented by a nil pointer.
}