News for hackers. Stuff that matters.
Re:Exploring Error Handling Patterns in Go by tln on Wednesday August 15, 2018 @10:28AM (#17766225)
I appreciated the article, but the error handling in Go just bugs me. So verbose...

The built-in tool does not even warn about unused errors... not `go build`, and not `go vet`. What's more important, an ignored error or an unused import?

https://play.golang.org/p/j-oXsZz51ki

Re:Exploring Error Handling Patterns in Go by pstuart on Thursday August 16, 2018 @12:04AM (#17771777)
That's by design -- programmer's choice.
Re:Exploring Error Handling Patterns in Go by TheDong on Thursday August 16, 2018 @01:09AM (#17772067)
I don't understand.

Go errors out on unused imports, but you can type "import _ foo.com/unused-import" to not error out.

Why doesn't 'errors.New("asdf")' error out and require you to instead write '_ = errors.New("asdf")' to ignore the result

I think the real answer is not that it's intentional design, but rather that the original compiler was not powerful enough to implement that feature easily... and once go hit 1., it was impossible for them to add new warnings or errors because there are no warnings and errors are backwards incompatible.

Sure, that means developers use third-party tools for warnings because the go compiler refuses to ever have warnings (that compromises the pure beauty of the language obviously), but at least that means it's only the users that have to deal with the complexity of using more tools, the compiler developers can ignore it.

Re:Exploring Error Handling Patterns in Go by mopsy on Thursday August 16, 2018 @06:22AM (#17773404)
I don't think it's because of the complexity. I'm quite sure it would not have been difficult to do.

One of the reason is that you don't always want to check the error. The most common one is fmt.Println.

I would not like to always write

_, _ = fmt.Println("Hello, playground")

Re:Exploring Error Handling Patterns in Go by stubish on Thursday August 16, 2018 @07:19AM (#17773650)
You certainly do not always want to write _,_ = fmt.Println("Hello, playground"), and I think this points out the real lack. What do you want your program to do if fmt.Println starts failing? I think, unless you are explicitly checking for errors, that in all other cases you want it to crash. Rather than silently continue. Which is a bug, and a potentially disastrous one, that is endemic in Go code (and, to be fair, plenty of other languages). Thankfully the practical risk of this particular case is tiny.

This is why you want unchecked errors to implicitly bubble up. Which is what exceptions give you, or perhaps a syntax with implicit error return values rather than Go's by-convention approach.

Re:Exploring Error Handling Patterns in Go by pstuart on Friday August 17, 2018 @11:50AM (#17783667)
If fmt.Println fails you've likely got bigger problems and the game is over.

And if it means a lot in this case, wrap the function and use it instead.

Re:Exploring Error Handling Patterns in Go by stouset on Thursday August 16, 2018 @12:57AM (#17772008)
And it’s—bluntly—a terrible design.
Re:Exploring Error Handling Patterns in Go by dfischer on Thursday August 16, 2018 @02:02AM (#17772303)
Why? I am genuinely curious. Terrible compared to what?
Re:Exploring Error Handling Patterns in Go by latch on Thursday August 16, 2018 @03:23AM (#17772620)
Exceptions.

Anders give an interview in 2003 [1] where he talks about how C# looked to learn from Java's checked exceptions. His conclusion was basically that, in their evaluation, 9/10 exceptions cannot be handled beyond some generic top-level handler.

If this observation is correct, and it certainly aligns perfectly with my own, then bubbling makes a lot more sense.

Note that, with error return values, you can emulate bubbling (which is what most Go programmers end up doing). And with exceptions, you can emulate return values. The question is what's the most common default? And, again, according to Anders, as well as any project I've ever worked on, bubble-by-default is overwhelmingly the most useful thing to support cleanly.

The only way Go's approach makes sense is if you consider it's original goal (system programming) and MAYBE (i don't know, I'm not a system programmer) for such systems you can/need to handle each error. Except that's not really how Go is being used now, so...

[1] https://www.artima.com/intv/handcuffs.html

Re:Exploring Error Handling Patterns in Go by jy3 on Thursday August 16, 2018 @04:10AM (#17772822)
I've always been annoyed by the parallel control flow introduced by exceptions in any language. They are used so often in many languages where it doesn't feel necessary.

The fact that I don't even have to think if the function call I'm looking at can throw and if I should catch it or not outweighs everything.

Re:Exploring Error Handling Patterns in Go by groestl on Thursday August 16, 2018 @06:18AM (#17773384)
Easy, just assume it throws. That's the case anyway. Thanks to panics, even in Go.

Edit: Also, there is no parallel control flow. Languages with exceptions have union-type return values, and every statement is implicitly followed by the equivalent of: if err!=nil return nil, err. The fact that in Go you have to type that makes Go cumbersome, not smart.

Re:Exploring Error Handling Patterns in Go by jy3 on Thursday August 16, 2018 @08:22AM (#17773984)
I'm not sure I understand the relationship between union-types and exceptions. You mean something like <Result, ErrorData>? Could you expand on that?

> Thanks to panics, even in Go.

Fortunately, for some reason Go developers don't use panic like exceptions and recover them at library boundaries.

Re:Exploring Error Handling Patterns in Go by groestl on Thursday August 16, 2018 @11:10AM (#17775288)
<Result, ErrorData>

Yes, exactly like that. In real world programs, every function might fail, even the simplest ones (stack overflow, out of memory, interrupts, etc). No information is gained by declaring a specific function might fail. Also it's almost certainly a lie to declare: this function will never fail. So if every function might fail, why not just produce a union type <Result|ErrorData> for every function return type. Also, lets automatically check for the error case after each nested invocation, and cleanly unwind the stack (returning ErrorData again) on failure. This makes 95% error handling code go away. The ErrorData type uses a special return keyword ("throw"), and in rare cases, errors need to actually be handled, so the union's ErrorData type is exposed to the user code with additional primitives (catch). On all the code in between, the ErrorData type is just hidden behind the scenes.

Re:Exploring Error Handling Patterns in Go by cube2222 on Thursday August 16, 2018 @06:47AM (#17773524)
Not really, in all the years I've been writing Go, only one library used panics for error handling.

Usually if something panics you don't want to handle it. (Other than at the http handler level, where you can just throw an InternalServerError and log the panic)

First and foremost, you can usually assume libraries won't panic, though it would be nice to have a tool (grep) to check for explicit panics.

Re:Exploring Error Handling Patterns in Go by groestl on Thursday August 16, 2018 @07:42AM (#17773768)
If something "errors", you usually don't want to handle it either. Other that at the http handler level.
Re:Exploring Error Handling Patterns in Go by cube2222 on Thursday August 16, 2018 @09:27AM (#17774404)
Actually, you often do. That's the point really. You should decide if it's an operation you might want to retry, you might also want to just flat out error and do nothing more, maybe you want to provide degraded functionality, like provide some default answer.

I think errors as values cause you to always think about this, which makes you handle errors in a more sensible way, instead of just bubbling up. Sure, 90% of situations you will bubble up, but in my opinion it's still worth it.

Re:Exploring Error Handling Patterns in Go by groestl on Thursday August 16, 2018 @01:05PM (#17776475)
You basically said so in your last paragraph, and I agree it's a tradeoff, I just want to reiterate that in my experience, bubbling up and retrying/degrading/failing at the top level (system boundary, client side) makes for a robust (distributed) system you can reason about. Having hundreds of easter eggs in the project where somebody tried to do something smart when encountering an error seems more like a nightmare scenario to me. Old Java enterprise applications are filled with this, and they are rightfully frowned upon. Checked exceptions are the culprit.
Re:Exploring Error Handling Patterns in Go by cube2222 on Thursday August 16, 2018 @06:52AM (#17773533)
I actually really like Go error handling (I write Go daily), but truth be said, they're a poor mans Either Monad.
Re:Exploring Error Handling Patterns in Go by pmarreck on Thursday August 16, 2018 @11:39AM (#17775594)
The thing that bugs me is that if you ignore the error (by simply not checking for it), it's still there, possibly insidiously corrupting runtime state. Imagine trying to debug a file format corruption that happened because some obscure part of the code tried to add to the format and instead errored (silently) and added garbage and then the code just kept chugging along until the state REALLY messed things up.

The thing many programmers don't seem to realize is that a program is a model of a design in the programmer's mind. If the model goes off the rails of the expected design/behavior in any way, that should be considered very bad ASAP... or as many languages treat it, "exceptional".

Re:Exploring Error Handling Patterns in Go by lozenge on Thursday August 16, 2018 @03:22AM (#17772614)
I used to forget to put "set -e" in bash scripts. Then one went off and deleted a whole bunch of important stuff despite a prerequisite command erroring. Now I include it, but remembering one line of code in the header is easy compared to Go's approach of remembering to check every error.