Go error handling is almost a meme. Who hasn’t seen a joke or two about the ocean of if statements for err != nil
?
Let’s consider a simple example:
|
|
What would be the output? True in both cases, obviously. Otherwise, it will break our beloved err != nil
flow.
Surprise, it’s actually not:
|
|
Go FAQ says:
Just keep in mind that if any concrete value has been stored in the interface, the interface will not be nil
So once we know the type, the interface is not nil anymore, as it holds type value internally.
References:
Sun Nov 26, 2023 / 190 words / Golang Programming Gotchas