Blog post about typed nils

Dave Cheney wrote a great blog post about typed nils:

We ends with:

For Go 2.0 I’d like to start the discussion of what it would mean if comparing an interface value to nil considered the value portion of the interface such that the following evaluated to true:

var b *bytes.Buffer
var r io.Reader = b
fmt.Println(r == nil)

What do you think?


I am completely on Dave’s side. I fail to see a use case for the way the nil comparison works today. It seems purely a result of technical considerations.

The GOPL has an entire subsection on this nil/non-nil dualism (7.5.1), and it starts with this remark:

A nil interface value, which contains no value at all, is not the same as an interface value containing a pointer that happens to be null. This subtle distinction creates a trap into which every Go programmer has stumbled.

'nuff said…

Edit: To anyone who wonders what “the GOPL” is: The Go Programming Language by Alan A. A. Donovan and Brian W. Kernighan, Addison-Wesley 2015.


Yes, this section from GOPL made me aware of this gotcha, too.

I wonder what would be necessary to change this behaviour for Go 2.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.