As usual in these threads about Go, I really wish people would consider Haskell as a nice alternative. A lot of people write Haskell off as "academic" or "impractical", which I feel is not an entirely fair assessment.<p>Particularly: Haskell is fast, concurrent by design (whatever that means, I'm sure Haskell is :P), typed but not cumbersome or ugly (less cumbersome and ugly than Go's types, even) and--most importantly for this article--does error handling really well.<p>In a certain sense, Haskell's main method for handling errors is actually similar to Go's. It returns a type corresponding to <i>either</i> an error or a value (this type, naturally, is called Either). This method is special, oddly enough, because it <i>isn't</i> special: Either is just a normal Haskell type so the error checking isn't baked into the language.<p>Coming from another language, you would expect to have to check your return value each time. That is, you fear your code would look like this pseudocode:<p><pre><code> if isError val1
then pass val1
else val2 <- someFunction val1
if isError val2
...
</code></pre>
However, this is not the case! The people implementing Either noticed that this was a very common case: <i>most</i> of the time, you want to propagate an error value outwards and only do any work on a valid value. So you can actually just write code like this:<p><pre><code> do val1 <- someValue
val2 <- someFunction val1
val3 <- someFunction val2
someFunction val3
</code></pre>
then, if <i>any</i> of the values return an error, it gets propagated to the end of the code. Then, when you want to check if you actually got a valid value--maybe in some central location in your code, or wherever is convenient--you can just use a normal case analysis.<p>Additionally, while the errors <i>do</i> pass through essentially silently, the type checker does ensure you deal with them at some point before using or returning them. If you ever want to get the Int from a Either Error Int, you have to either handle the error case somehow or explicitly ignore it (e.g. with a partial pattern match). The latter option will generate a compiler warning, so you can't do it by accident without being notified.<p>So the mechanism is simple, but it can also stay out of your way syntactically. So what else is this good for? Well, it's just a normal data type, nothing special; you can use existing library functions with these values. For example, you can use the alternation operator <|> to find the first non-error value:<p><pre><code> val1 <|> val2 <|> someFunction 5 <|> ...
</code></pre>
This is often a very useful idiom which would be harder to write with a different way of handling errors. There are more utility functions like this (e.g. optional) that let you make your intents very clear at a high level. The optional function, for example, lets you do exactly what it claims: you can mark a value as "optional", meaning that any error from it will be ignored rather than propagated.<p>You can also layer on this error-handling logic on other similar effects. For example, there are some types (like LogicT) that represent backtracking search. Combining error-handling with nondeterminism gives you a question: should an error cause the <i>whole</i> computation to fail, or only that particular branch? The beauty is that you can choose <i>either</i> option: if you wrap LogicT with ErrorT (this is just like Either except it can be combined with other types) an error will cause the <i>whole</i> computation to fail; if you wrap ErrorT with LogicT, the error will only cause the current branch to fail. This not only makes it easy to choose one or the other, but also makes it very clear which one you <i>did</i> choose: it's right there in the type system in a very declarative fashion.<p>Haskell also has a bunch of other advantages which aren't worth going into here. I think anybody looking for something like Go--a fast, high-level, concise, typed alternative to Python--should <i>definitely</i> consider Haskell. While it may not seem so at first, I believe that Go and Haskell are good for a very similar set of problems and so actually overlap quite a bit.<p>If you really dislike some particular things about Haskell, you should also consider some similar languages like OCaml and F#. I personally prefer Haskell, but there are definitely good cases to be made for either of the other two.