Re:Having fun with Go's nil, interfaces and errors
by SirWart on Saturday September 17, 2016 @02:19PM
(#12521672)
I got bit by this the other day when returning a nil pointer to a struct as an error. It was incredibly frustrating when I finally figured out what was going on, and I'm curious if there's a good reason to distinguish between these 2 types of nil or if it was just an oversight/mistake during the language design process.
[ Parent
| on HN ]
Re:Having fun with Go's nil, interfaces and errors
by amscanne on Saturday September 17, 2016 @03:40PM
(#12522066)
It's not two types of nil.
When a nil value is converted to an interface, it carries type information. When an interface carries type information, it isn't nil.
Re:Having fun with Go's nil, interfaces and errors
by catnaroek on Saturday September 17, 2016 @02:33PM
(#12521741)
Most likely the reason is keeping the language implementation simple. Java-style `null` is actually quite subtle: http://stackoverflow.com/a/2707333/46571
Re:Having fun with Go's nil, interfaces and errors
by pcwalton on Saturday September 17, 2016 @05:29PM
(#12522556)
Go has the same complexity. From the Go spec: "For an expression x of interface type and a type T, the primary expression x.(T) asserts that x is not nil and that the value stored in x is of type T.'"