|
|
|
|
|
by dilap
3569 days ago
|
|
I don't think it's worth breaking compat, but I agree it would've been nice and saved a ton of confusion. Right now a pointer to a type that is zero is called "nil". Similarly, an interface that contains zero for both the type and the value is called "nil". This is really confusing because var foo SomeType = nil
var bar SomeInterface = foo
fmt.Println(bar == nil) // prints false, which is confusing
If instead you called the zero-value of interface something else, say "unset", it would be a lot less confusing. var foo SomeType = nil
var bar SomeInterface = foo
fmt.Println(bar == unset) // prints false, which makes sense, because bar is set -- to (nil, SomeType)
I first encountered this idea in this reddit thread:https://www.reddit.com/r/golang/comments/4injtt/understandin... |
|
In Go nil is a special identifier that represents a "zero value" for several types (pointers, interfaces, functions, slices, channels).
"unset" would be a "zero value" for interfaces so that nil could be "interface that has a type but whose value is an zero value of a pointer or function or slice or a channel.
This is inconsistent - now you have 2 identifiers to represent a concept of "zero value".