3 ms·
I like explicit error handling, but I wish there were a concise way to handle errors from the same line. A one-line function call `foo()` becomes four times as
by bccdee 5y ago
I like explicit error handling, but I wish there were a concise way to handle errors from the same line. A one-line function call `foo()` becomes four times as long once you handle errors.
result, err := foo()
if err != nil {
return nil, fmt.Errorf("Error message: %w", err)
}
This pattern is just so commonplace, but it's four times as long.
Contrast with Rust, which handles errors as `foo()?` or `foo().context("Error message")?`*. It's still explicit — I like it better than silently-propagating exceptions — but my functions don't wind up being 75% error handling.
* Where the `context` function comes from an error-handling library.