The value ends up being a non-nil interface value that holds nil.
To avoid encountering this issue, return `error`, not something else.
Shadowing in a different scope allows it to work properly as well.
I admit that I've never actually run in to this issue in practice as I generally return custom errors as `error`, but I can see why it would confuse someone.
But best practice is to return concrete types.
Also, `err` is just an example in this minimal snippet. It can happen with any variable.
Here's a similar snippet: https://play.golang.org/p/WKPey_PL_ht -- Looks impossible to crash. :P
Spoiler: it's a boxed null, not a null.
You're taking this as an absolute when you shouldn't be. There's plenty of places in the standard library that don't follow this "rule".
You can always unwrap it via type assertion, however ugly that may be.
Except for errors. Use the `error` interface.
I've never really cared about Generics, but having nullable types is a real mistake to me.
Nullable types by themselves are fine, the language and the compiler just needs to make handling “is X null?” work the same way as option types do - or better yet: languages were a variable is only in scope if it is not null.
https://play.golang.org/p/N_3BiUOkRJo
It's probably the first and last to have this quirk.