29 points · by ibraheemdev · 5y ago · 27 comments
Error handling in Go is similar. Errors are not something that live outside the flow of control. When they’re out of sight, they become like the fire escape with a barbecue on it or that’s covered in snow in the winter. Even if they’re just being passed up the call stack, they’re at least being handled and you can see exactly how. If they’re not being handled, static analysis can tell you.
It’s not perfect. I’ve had times where I’ve grown tired of writing `if err != nil`. The difference is at least I do it more often in Go, rather than scratching my head trying to figure out bespoke error management in projects written in other languages or whether my error is worthy of being an Exception.
EDIT: added link to 99PI
Truth is that the canonical way of handling errors in Go, despite looking unsightly to people from other language ecosystems and seeming cumbersome to use, actually works well in practice.
The fact that when used along with a linter that enforces error handling, I never have to worry if a certain function I'm using can `throw` and not having to wonder whether that has been handled in a try-catch provides a lot of cognitive relief.
I say this as someone who loves other languages like Rust as well. The fact that there is a canonical mechanism that everyone uses which you don't need to second guess, or jerry rig your own is a great net benefit. In Rust, it's the union Result type, in Go it's every function returning error. I would not advocate for one trying to shoehorn the other's mechanism into its own ecosystem.
type error interface { Error() string }
and then
if err != nil { return err } 10000x times
counts as a legitimate error handling strategy, you either have an extremely low bar set or haven't used Go much. Then again, people seem to be okay with Golang allowing nil pointer exceptions to exist in 2021, so maybe the bar is underground.
Go error handling in practice actually works very well. I've been writing Go for a long time and I've never had an issue with it. It's not as fancy as Rust but it works and works well, so if it ain't broke, please don't try fixing it.
The weakness in the current design is three-fold for me: - I can "forget" to handle in error (ok this can be linted for maybe?) - There is a lot of ceremony in propagating errors up the chain (a very very very common thing to do!) - The convention of (ok, err) is not always conformed to and ultimately provides no benefit over more concrete typings like Result<T, Err>. Now I can easily grep for fallible operations or operations that call fallible operations!
You may be fine with it - I hear the same argument from my coworkers about checked exceptions in java. I think we can do better.
I disagree. A quick look through the Go code I'm working with now shows more than half the functions that check for and return errors have absolutely no business knowing about the error. These places are in the middle of the call chain, well away from the place that generated or first encountered the error. These functions check the "if err != nil" and bubble them up. That's just noise. Go's error handling in these cases color everything in their path.