This was a funny feature I came across when i was learning Go. I was working with an API that used interfaces and was surprised that some of my code did not behave the way I expected.
This was due to the fact that in Go, a typed variable that is set to nil actually is not nil but rather an object containing the type and the value (nil). Hence when you compare with nil it is never equal to nil.
Confusing? Sure is. These two functions show the difference:
And if you want to execute it you can do that here. You can also read the official explanation here.
I've been thinking about how I feel about this. To me this is definitely one of those cases where the bare bone puristic approach a lot of things have in Go bubble up. Technically nil of a certain type is not the same thing as nil of unknown type. And I can see how the implementation made this approach easy to implement. But there are two options that would make more (or less) sense.
First option is to consider nil nothing and use the infinity analogy that nothing compared to nothing is still false. SQL implementations typically take this approach where anything compared with NULL is NULL.
But that option wouldn't be that useful and we could rather see the atom nil as all types rather than no types meaning that a nil of any type will be equal to a nil of any other type. Or at least the untyped nil would be equal to all typed nils. This approach would definitely make a lot of things in Go appear more like in other languages and is technically not wrong either. I really wish they would have taken the latter approach as the literal nil is kind of a special case either way.