Repository navigation
Description
A common error for people new to Go is misunderstanding that a interface that is not nil can contain a nil pointer. This is one of the most commonly cited entries in the Go FAQ: https://golang.org/doc/faq#nil_error. A quick search shows at least 24 threads on golang-nuts discussing this, even though it is already in the FAQ.
It is not new to observe that one of the causes of this common mistake is that nil is overloaded. Since changing that would not be backwards compatible, I propose the following changes.
-
We add six new predefined identifiers:
nilptr,nilinterface,nilslice,nilchan,nilfunc,nilmap. These new identifiers are untyped constants that only resolve to a certain kind, much as1.1is an untyped constant that only resolves to afloatorcomplextype.nilptrmay be used as the zero value for any pointer type,nilinterfacemay be used as the zero value for any interface type, and so forth. An attempt to use, for example,nilptrin an assignment or comparison with a variable or value that is not a pointer type is invalid. -
We add a vet check that warns about any comparison of a value of any type with plain
nil. We encourage people to changex == niltox == nilinterfaceorx == nilptror whatever is appropriate. Since initially this vet check will trigger all the time, we can not turn it on when runninggo test. It could be on by default when runninggo vet. -
At this point people who run
go vetwill no longer make the common mistake. Ifvis a value of interface type, then writingv == nilptrwill be a compilation error. Writingv == nilinterfacewill not cause people to erroneously think that this is testing whethervcontains anilpointer. -
In some later release, we turn on the vet check when running
go test. -
If we are ever willing to make a backward incompatible change, we can make
v == nila compilation error rather than simply being a vet error.
Something to consider is that one could imagine permitting v == nilptr when v has an interface type, and having this be true if v is not nilinterface, if v holds a value of pointer type, and if the pointer value is nilptr. I don't know that this is a good idea, and I'm not proposing it. I'm only proposing the above.
Activity
cespare commentedon Nov 15, 2017
One consequence of this would be that the extremely common
if err != nil { ... }would be significantly longer:if err != nilinterface { ... }.ianlancetaylor commentedon Nov 15, 2017
@cespare Very valid point.
bradfitz commentedon Nov 15, 2017
I'd be happier with something in the middle: only adding
nilinterface, but naming itnoneor something shorter. I don't thinknilmap,nilslice, etc generate enough confusion.ianlancetaylor commentedon Nov 15, 2017
@bradfitz It sounds like you are suggesting that we move toward a different name for the zero value of an interface type, but keep the general overloading of
nilfor non-interface types. Does that sound right?I don't really see
noneas conveying the right idea, but I'm having trouble coming up with a good name. We could tryniliorinil, but those might be too close tonil. Maybeempty? I hope someone else has a better idea.(C++ has a similar confusion in which
0is the zero value for both integers and pointers, with the preprocessor macroNULLoften being used in code. In C++ this has now been resolved by preferring the new keywordnullptras the pointer zero value.)bradfitz commentedon Nov 15, 2017
Yes.
I like
empty.Seeing lots of
if err != empty { ... }is still pretty verbose, though.cznic commentedon Nov 15, 2017
emptyis nice, and it would be enough to predeclare it in the universe scope without breaking any existing Go1 program.Playground
cznic commentedon Nov 15, 2017
Which is actually what the proposal says, but I noticed that only now, mea culpa 👎
davecheney commentedon Nov 15, 2017
@ianlancetaylor thank you for raising this issue. I think this proposal adds a lot of complexity to solve a single issue, returning a typed nil. What about just making a typed nil equal to
nil? It is after all what the author of the check intended.bradfitz commentedon Nov 15, 2017
That breaks valid code.
neild commentedon Nov 15, 2017
If
interface{}((*T)(nil)) == nil, how do you test to see if an interface value is zero? I also think changing this equality might be superficially more intuitive, but is more confusing when you dig into it.Instead of changing the name of a zero-valued interface, it would also be possible to change the name of a zero-valued pointer.
null,nullptr, or maybe just0.davecheney commentedon Nov 15, 2017
39 remaining items
andig commentedon Apr 10, 2020
This is a little off-topic, but since https://golang.org/doc/faq#nil_error was mentioned I feel it's not precise enough:
It seems that the bad (first) example is "use(ing) the error type in their signature" as advised, yet still broken. It should not only use the error type but also return the nil interface instead of a differently-typed nil (if that's the correct wording).
rogpeppe commentedon Aug 24, 2020
@griesemer wrote:
I've been thinking about this for a long time, and while I buy the theoretical argument, I'm still not entirely convinced.
I haven't yet seen many significant cases where this property is used to advantage.
One point in favour of disallowing typed nils as valid receivers: the pointer method set also contains the value method set, but if you happen to call a value method when the pointer is nil, you'll get a panic, so that property isn't as useful as it could be. Passing the zero value is one possible way of fixing that, but disallowing nil receivers would be another. If you disallow nil receivers, then perhaps it's reasonable to change a typed nil pointer into an untyped nil when converting to an interface. The problem is much less acute for slices, channels and maps, so perhaps they could be left as is.
But as @ianlancetaylor points out in the above-mentioned issue, doing this would change the behaviour of existing code, which probably rules it out as a language change.
tandr commentedon Aug 24, 2020
Would completely disallowing a naked (uncasted)
nils in comparisons solve this? Like, enforce a rule thatnilcannot be naked, it has to be dressed in a type wrapper. (if only for "complex" type comparisons at the beginning)go vetshould be able to help with transition.Sorry, I don't remember if it was suggested and declined as "too much existing code affected".
ianlancetaylor commentedon Aug 24, 2020
@tandr Anything that requires us to rewrite
if err != nilis a non-starter. (That is also a problem with this proposal.)earthboundkid commentedon Aug 24, 2020
The options are:
Whichever is done, for backwards compatibility reasons p == nil and err == nil can probably never be changed, but you could add a lint check + rewriter to used the preferred one.
On identifiers, my two cents are that
nilinterfaceis too long to type regularly. (interface{}is already long enough, and you don't have to type it that often.)nullis good on its own, but probably too close tonilif we have both in the language.noneseems fine. So, I lean towards, just addnonefor interfaces and get a rewriter forerr == niltoerr == none. You could also makenonevalid for map/slice/chan, but I don't think that solves a real problem, so I would lean against it.andig commentedon Aug 24, 2020
To pick up thw comment by @tandr: why to we need the ability to invoke methods on a nil value at all? I think I understand why its technically possible and permitted by the language as-is, but is it necessary for any purpose?
xuanswe commentedon Aug 11, 2023
I did not read all comments in this very long topic, sorry for that.
I am also new to this language, so sorry if I don't understand the details.
But is that the same story as Dart?
Dart 1: no null-safety
Dart 2: null-safety feature is optional. migration-tool is provided.
Dart 3: null-safety feature is a must
Why don't we apply this successful story to Go?
ianlancetaylor commentedon Aug 11, 2023
@xuan-nguyen-swe This issue doesn't have anything to do with null safety. That is a completely different topic. For null safety see, for example, #28133.
xuanswe commentedon Aug 11, 2023
@ianlancetaylor thanks for redirecting me to the correct topic!
I am new to Go, so please apologies to me if I misunderstood something.
[-]proposal: Go 2: add kind-specific nil predeclared identifier constants[/-][+]proposal: spec: add kind-specific nil predeclared identifier constants[/+]