6 ms·
Thanks! Go doesn't have exceptions and it is idiomatic[1] to return the error type. Dave Cheney has a really great blog post[2] which he describes why go does a
by anonfunction 11y ago
Thanks! Go doesn't have exceptions and it is idiomatic[1] to return the error type. Dave Cheney has a really great blog post[2] which he describes why go does away with exceptions. The Golang wiki on GitHub has a great section[3] on errors as well.
1. https://gobyexample.com/errors https://gobyexample.com/errors
2. http://dave.cheney.net/2012/01/18/why-go-gets-exceptions-right http://dave.cheney.net/2012/01/18/why-go-gets-exceptions-rig...
3. https://github.com/golang/go/wiki/Errors https://github.com/golang/go/wiki/Errors
- dragonwriter 11y ago> Go doesn't have exceptions Go has panics, which are pretty much the same thing as exceptions in other languages (the details of how they are used are slightly different from, say, Java/Python/Ruby exceptions, but much more than, say, Perl 6 exceptions.) Go has a strong stylistic recommendation not to expose panics to external consumers of code, and it is idiomatic to instead use error returns from publicly-exposed library code. But "Go doesn't have exceptions" is mostly misleading.
- anonfunction 11y agoRob Pike, one of the three creators of Golang, disagrees with you. In his blog post less is exponentially more[1] he shares a list of significant simplifications in Go over C and C++ which includes: > no exceptions 1. http://commandcenter.blogspot.pt/2012/06/less-is-exponentially-more.html http://commandcenter.blogspot.pt/2012/06/less-is-exponential...
- dragonwriter 11y agoAnd yet "panics" exist, with very similar semantics to exceptions in C++/Java/Python/Ruby. Sure, there's differences between Go panics and any one of those implementations of exceptions (just as there are between them, or between any of them and Perl 6 exceptions.) But panics are clearly a variation on the exception theme, just as each of those forms of exceptions is. The strong idiom of not having panics exposed across public interfaces of libraries is a cultural change, which to the extent that it is followed does present a simplification compared to ecosystems where you have to deal with both returns and exceptions in library code. But panics very much are exceptions, and changing the name doesn't change that.
- anonfunction 11y agoAs I was originally answering a question regarding whether it was normal to return errors or raise exceptions, and because I've read Dave Cheney and Rob Pike's blog posts explicitly stating Go does not have exceptions I replied as such. However, after doing some research I learned that in addition to Panic() there's also Recover()[1], so I'll concede that there is similarity between panic/recover and throw/catch. Prior to my learning about Recover() I was under the assumption that Panic was just used to print a message and close a program with a non-zero status. 1. https://github.com/golang/go/wiki/PanicAndRecover https://github.com/golang/go/wiki/PanicAndRecover
- libria 11y ago> "Go doesn't have exceptions" is mostly misleading. It's worded too strongly perhaps. While `panic` behavior is similar to `java.lang.Exception`, it is not idiomatic to panic on problems like IOException/SQLException. They are detected by return codes to be handled immediately, rather than a broad swath of code wrapped in `try`. This in Java: try code code code catch errA handle catch errB handle catch errC handle Would be this in Go: code handle errA code handle errB code handle errC It's fair to say "Go doesn't use exceptions" since Go doesn't use panics for common error handling.