Yes, there's some of this:
if f, err := io.Open("foo.txt"); err != nil {
return err
} else { // use f
}However, that's not really any more code than
try:
f = open("foo")
// use f
except IOError as e: return e // or throw, or whatever
Plus Go uses multiple returns everywhere, so you don't have to worry about errors being confused with actual data returned.It's pretty awesome.
This means you can't continue to execute the function, but you do get a chance of handling the exception / turning it into a regular return value.
tl;dr: Go has exceptions, they are just a little different (IMO for the better).
In the Go standard library source code, "panic" has pretty much the role 'assert(!"error message")' has in C.
The reality is that Go does not have exceptions the way Python does. If you're writing idiomatic Go, you're manually checking errors.
Exactly (and probably for a reason)
> In the Go standard library source code, "panic" has pretty much the role 'assert(!"error message")' has in C.
Well, there's no real way to recover after such an assert (the C stdlib is undefined when you catch the SIGABRT, the stack is not guaranteed to be big enough to do anything, most I/O is not guaranteed to work, etc. -- really, very few uses for catching a SIGABRT). There is a way in Go, even if it is far less capable than your average "except .." clause in C++.
> The reality is that Go does not have exceptions the way Python does.
True. Some of us (e.g. me) think that's a big advantage. (I'm not implying you disagree - I don't know what your opinion is)
I just find that so much more pleasant.
Even though I'm using GoSublime I haven't really dug into its plethora of feature, it might just have a repl in there...
Yep, rolled my own here: http://github.com/metaleap/go-buildrun -- I use it with GoSublime on Ctrl+B (which I strongly suspect must have some repl-like feature anyway by now, but not sure) -- but works in any editor that lets you hook a key binding to an executable and pass it the current .go file's path.
I have been holding back because I don't like the lack of exceptions. But it occurred to me that C doesn't have anything like exceptions either, and even though that lack causes some serious irritation, the C language as a total package overcomes it and is really powerful.
I don't know if any language can beat that list of tool chain benefits. Maybe I will give go a whirl.