3 ms·
> Go is strongly and statically typed with no implicit conversions This is one glaring exception: Concretely typed objects can be silently converted to interfa
by oconnor663 7y ago
> Go is strongly and statically typed with no implicit conversions
This is one glaring exception: Concretely typed objects can be silently converted to interface objects. In particular, a `*MyErrorStruct` can be silently converted to an `error`. The giant footgun associated with this conversion is that because the resulting interface object "remembers" its concrete type, it won't compare equal to `nil`, even if the original value was in fact `nil`.
This is a pretty big exception, which I've seen cause bugs in production. It's common to have one function that returns a concrete type wrapped by another function that returns an interface. That means the silent type conversion is happening in the `return` statement, which is extremely easy to overlook. (Or which can be introduced after the fact by changing the signature of either function.)