5 ms·
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
by SirWart 10y ago
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.
- catnaroek 10y agoMost likely the reason is keeping the language implementation simple. Java-style `null` is actually quite subtle: http://stackoverflow.com/a/2707333/46571 http://stackoverflow.com/a/2707333/46571
- pcwalton 10y agoGo 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.'"
- amscanne 10y agoIt'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.