The Go Programming Language turns two.
I really want to like Go (Commander Pike is one of my heroes, and systems programming currently either means C or a Special Olympics scripting language). But I'm slightly put off by the lack of exceptions, and the seemingly gratuitous concepts offered instead. Does anyone have experience with how this aspect of Go pans out in real world use?
It added panics later which is kind of exceptions by another name
ReplyDeleteI write a lot of Go and have mixed feelings about its error-handling. However, I wouldn't change anything for lack of a better alternative.
ReplyDeleteMaybe panic/recover are the "gratuitous concepts" mentioned? Recovering from a panic is intentionally a little inconvenient to discourage panics for everyday type of failures (file not found, etc.).
Panics should happen only when there's a bug in the code. recover is there to save you from buggy code you didn't write or to make sure that your server never crashes, even if it fails.
I think it's true that exceptions are abused in many languages. They occur all the time for very mundane reasons. I also think back on Raymond Chen's blogs on exceptions[1][2] and find it hard to disagree.
At the same time, error codes are tedious. 90% of the time you're just letting errors trickle up to the caller and that's something exceptions do very well. Go's GC and defer mean there's less cleanup and fewer levels of indentation than the same code in C, but it's still painful at times.
[1] http://blogs.msdn.com/b/oldnewthing/archive/2004/04/22/118161.aspx
[2] http://blogs.msdn.com/b/oldnewthing/archive/2005/01/14/352949.aspx