by swort · 10y ago · 0 comments
https://golang.org/doc/faq#nil_error
Of course, there are ways around this such as returning a struct, but then that's no longer compatible with the error interface.
Which your struct can easily fulfill. That's the beauty of Go interfaces.
This is perfectly valid and doesn't cause the nil issue:
return someErrorStruct{}
...where someErrorStruct is a strict that implements the "error" interface. Using structs for errors is fine, and is in fact generally preferable to singletons like io.EOF, which can (by their very nature) never be extended with more data about the error.There's nothing absurd about it; interfaces are a reference type. If you have a reference to a reference, then checking the "outer" reference for nil doesn't tell you anything about the nullity of the "inner" reference. The advice is just a special case of "don't needlessly use pointers-to-pointers".