Is returning error common in go (as opposed to raising)?
Is returning error common in go (as opposed to raising)?
1. https://gobyexample.com/errors
2. http://dave.cheney.net/2012/01/18/why-go-gets-exceptions-rig...
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.
> no exceptions
1. http://commandcenter.blogspot.pt/2012/06/less-is-exponential...
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.
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.
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.