Why Go Can't Try

10 points by telemachus


justinpombrio

This is LLM written, gets at least a couple minor things wrong, gets the main thesis ambiguously wrong, and buries the lede in an annoying way.

At first glance it seems simple. try isn't a keyword in Go today, so adding it wouldn't break existing code.

It would break any existing code that uses try as a variable name. To add a new keyword without breaking existing code, it needs to be reserved by the language for future use, so that it's an error to use it as a variable name. Whether Go does this, who knows, because no one looked it up, because the article was written by an LLM.

They've historically drawn a line between this and exceptions, arguing both make control flow hard to follow.

That's the opposite of what drawing a line between two things means.


The thesis is that Go can't just add a try keyword to reduce boilerplate. The article says this directly and indirectly in a few places. But Go could add try; there's a simple and fully fleshed out proposal for it:

https://github.com/golang/proposal/blob/master/design/32437-try-builtin.md

Here's the actual lede which is buried:

Here's the uncomfortable truth: a try keyword in Go without fixing the error type is just syntax sugar. You'd get slightly less typing but none of the real benefits - no exhaustiveness checking, no compiler-inferred error sets, no guarantee that you've actually handled every case. It would make control flow less visible while delivering only a fraction of what makes Zig's approach genuinely good.

Yes, that's correct: adding a try keyword (i) would mean that when you see try, that's control flow that flows to the end of the function just like return, and (ii) would not magically allow Go to start doing exhaustiveness checking on error messages the way that Zig does.

Marked as spam. We really need a "slop" tag. I don't even care if something is LLM written, but "looks good on the surface, actually has lots of holes" is really common now, and there really ought to be a way to mark it.

colonelpanic

This is good analysis, I've been thinking for a while that people are wrong about Go's error handling. They correctly intuit that it's bad, but focusing on the if err != nil pattern is misplaced; it's the type system that's the real problem.

Another issue not discussed here are the error returns from functions. They are written like a tuple, but the language can't express that in a type, so you are forced to handle it manually.