News for hackers. Stuff that matters.
Re:Error stack traces in Go with x/xerror by johnmaguire on Monday August 23, 2021 @08:37PM (#28283855)
> With explicit error handling, however deep your call stack is, you are relying on everything in that stack to have done the right thing with regards to error handling. With exceptions, you have to go out of your way to screw them up.

I don't think I agree with this argument. In many languages, there's no obvious indicator that any given function may or may not raise / throw an exception. And even if it doesn't throw an exception today, it might tomorrow, and it's easy to forget to update all callers.

Since Go makes errors a return value you have to actively discard the result (by replacing it with a underscore, e.g. `res, _ := getResults()`) or take the time to handle the error.

And just like an intermediary library in Go can swallow the error, so can an intermediary library in an exception language catch and discard an exception.

It seems to me that the result is more errors are properly handled in Go - because they are explicit - while uncaught exceptions often cause bugs that make it to production.

For context, I recently switched jobs from one where I wrote Python for 4.5 years to a job where I've been writing Go for about 5 months.

Re:Error stack traces in Go with x/xerror by suresk on Monday August 23, 2021 @08:43PM (#28283882)
> With Go making errors a return value you have to actively discard the result (by replacing it with a underscore, e.g. `res, _ := getResults()`) or take the time to handle the error.

Right, but because `err` ends up getting re-used in so many cases, you can re-assign it and forget to do anything about it (which is what I've found in most cases where an error was inappropriately suppressed in Go).

In most cases, simply bubbling up the error to something that will generically handle all errors is the right thing to do, and in the case of exceptions, even if you don't know about one, that is what will happen. I just sampled a handful of the top Golang repos on Github, and found very few cases of anything more than the standard `if err != nil; return nil, err' pattern that just bubbles the error up.

Re:Error stack traces in Go with x/xerror by anonymoushn on Tuesday August 24, 2021 @03:47AM (#28286071)
Last time I wrote go you could write `doThingAndMaybeReturnErr()` without the `_ =`. Has this changed?

The alternative is Zig or Rust's thing, where you still have explicit error handling but you don't repeat the same four lines all the time. This is maybe helpful because in Go one has to read those lines carefully to see if anything unexpected is happening.

Re:Error stack traces in Go with x/xerror by papaf on Tuesday August 24, 2021 @06:55AM (#28287126)
Linters such as golangci[1] which will warn if you ignore the return value. If you ever write Go again I highly recommend you look at golangci -- the security checks have been really helpful to me.

[1] https://golangci-lint.run/

Re:Error stack traces in Go with x/xerror by n_e on Tuesday August 24, 2021 @05:01AM (#28286459)
> Since Go makes errors a return value you have to actively discard the result (by replacing it with a underscore, e.g. `res, _ := getResults()`) or take the time to handle the error.

LoadData(&data)

Does this function return nothing or an error ?

Re:Error stack traces in Go with x/xerror by cy_hauser on Tuesday August 24, 2021 @05:31AM (#28286639)
It returns nothing or the compiler/linter/ide will tell you.
Re:Error stack traces in Go with x/xerror by tsimionescu on Tuesday August 24, 2021 @05:50AM (#28286764)
Which compiler/linter/IDE is that? The Go compiler and linter certainly don't tell you:

https://play.golang.org/p/k-eTs3dC_wK

Note that Go playground also runs go vet, a static analysis tool.

Re:Error stack traces in Go with x/xerror by cy_hauser on Tuesday August 24, 2021 @07:09AM (#28287220)
You're probably right. I use Goland and I just think of it as a natural extension to Go. I shouldn't.
Re:Error stack traces in Go with x/xerror by kortilla on Tuesday August 24, 2021 @03:45AM (#28286065)
> Since Go makes errors a return value you have to actively discard the result (by replacing it with a underscore, e.g. `res, _ := getResults()`) or take the time to handle the error.

This is a bad thing because it’s not narrow. It’s like a language’s “catch” statement not allowing you to catch specific exceptions. Once someone has decided to throw away an error with the underscore assignment, all future errors the underlying might return are swallowed as well.

In other words, it takes even more boilerplate to have narrower error exemptions than to have the generic exemption.

> It seems to me that the result is more errors are properly handled in Go - because they are explicit - while uncaught exceptions often cause bugs that make it to production.

An error in go that is blindly returned all of the way up the stack has no difference with an uncaught exception.