It's true that functions that return (result, error) tuples are hard to miss the errors from, because you have to explicitly either at least accept it, or ignore it. It's both visually obvious that you've dropped the error and there are linters to catch this for you (which I highly recommend integrating into pre-commit hooks).
However, if a function just returns an error, but no result value, it is indeed easy to silently drop that error without realizing it by simply invoking the function and not catching the result at all. To which, again, I'd highly recommend using linters at commit time or even code save time.
> In Go, if a function returns an error you have to explicitly handle or suppress it. Otherwise code just doesn't compile.
ah, yeah, great idea. in practice this just means that most Go projects are minefields of
if err != nil { panic(err) }
or
if err != nil { return err }
ie a manual & repetitive implementation of assert for the first case and manual & repetitive implementation of an exception call stack for the second. thank you very much, I'll take the automated version known as exceptions instead.
whateveracct|7 years ago
hu3|7 years ago
There's no margin to "forget".
jerf|7 years ago
However, if a function just returns an error, but no result value, it is indeed easy to silently drop that error without realizing it by simply invoking the function and not catching the result at all. To which, again, I'd highly recommend using linters at commit time or even code save time.
jcelerier|7 years ago
ah, yeah, great idea. in practice this just means that most Go projects are minefields of
or ie a manual & repetitive implementation of assert for the first case and manual & repetitive implementation of an exception call stack for the second. thank you very much, I'll take the automated version known as exceptions instead.weberc2|7 years ago
masklinn|7 years ago
If a function only returns an error, or if it returns both a value and an error and you care for neither, you can absolutely ignore them entirely.
And when you can "suppress" an error and still access the value, it's not really impressive.
insertnickname|7 years ago
[0] https://godoc.org/fmt#Printf
silasdavis|7 years ago