News for hackers. Stuff that matters.
Re:Quick and Clean in Go by masklinn on Wednesday August 01, 2012 @03:00PM (#4324910)
In this precise case, the alternative would be:

    http.ListenAndServe(":8080", nil)
the uncaught exception would kill the task because, really, what is your http server going to do aside from exit if it can't serve?

Not sure why you added a bunch of lines which end up doing the exact opposite of what the original code does.

Re:Quick and Clean in Go by romaniv on Wednesday August 01, 2012 @03:39PM (#4325161)
Exactly. It horrifies me that there are still programmers who don't understand this after so many years of OOP advocacy.

The benefits of a properly designed (not Java-like, but C# or PHP like) exception system stems from the fact that in the wast majority of cases you don't need to catch exceptions right away. Most exceptions can be handled in a few centralized locations higher up the stack. If you have 10 calls to ListenAndServe, this means having 10 times less exception handling code.

Moreover, when you design a library and the users actually care about errors, exceptions save you from needing to pass your error codes up the call chain from deeply nested methods.

Re:Quick and Clean in Go by zemo on Wednesday August 01, 2012 @05:06PM (#4325705)
>If you have 10 calls to ListenAndServe

ListenAndServe blocks forever. If you have 10 calls to ListenAndServe, it's implied that you're in 10 different, concurrently running processes (goroutines). A panic, in Go, does not cross a goroutine boundary. If, in imaginary parallel universe Go with exceptions, exceptions did cross goroutine boundaries, then one goroutine could crash another goroutine.

The point of Go is to be able to sensibly write stable, concurrent programs with compile-time safety. Exception-based error handling would be a mess with Go's concurrency model.

In practice, you would only call ListenAndServe 10 times if you were listening on 10 different ports, because you had 10 different projects on the same server. That's a perfectly realistic situation. Now consider that one site has an error and throws an exception. Should all the other sites go down?

Error handling in Go is admittedly tedious, but when you work with the language for a bit you'll start to understand why they chose to design it in that manner.

Re:Quick and Clean in Go by flogic on Wednesday August 01, 2012 @07:13PM (#4326287)
If Panic doesn't cross goroutine boundaries, why should exceptions?
Re:Quick and Clean in Go by drivebyacct2 on Wednesday August 01, 2012 @07:24PM (#4326338)
The whole point is, it wouldn't make sense for them to cross goroutine boundaries, but the pleasure of Go is the ease with which you can use goroutines. Thus, in a lot of places where people would lazily use exceptions to aggregate potential errors, you would be unable to because the exception is occuring in a goroutine.

Which is why there are other nice ways of communicating such states in go that go hand in hand with handling errors at their occurrence. If you find an error in a goroutine, you can signal that on a channel, return and wait for a new goroutine to be restarted in place of the errored one.

Re:Quick and Clean in Go by tomjen3 on Wednesday August 01, 2012 @03:22PM (#4325044)
Log the error to the logging system (may also fail) and try again, upto a specific number of times?
Re:Quick and Clean in Go by thebigshane on Wednesday August 01, 2012 @06:49PM (#4326198)
If you don't care about the error (because you're just going to let the task die anyway). Then in Go, wouldn't you just write...

  _ := http.ListenAndServe(":8080", nil)

?
Re:Quick and Clean in Go by drivebyacct2 on Wednesday August 01, 2012 @07:02PM (#4326246)
You get a syntax error, "no new variables on left side of :=".

You could do:

    _ = http.ListenAndServe(*port, nil)
But why would you? Just omit it entirely. The only time you need to explicitly ignore errors is in multiple returns.

consider: http://play.golang.org/p/o8vLQQmRvp

which would be fixed like: http://play.golang.org/p/DH0j1cwRqW

But yes, since ListenAndServe is normally used to block before main() returns, you could let it fail and terminate. (But you'd probably want to exit with a non-zero value, at least I would).

Re:Quick and Clean in Go by drivebyacct2 on Wednesday August 01, 2012 @04:09PM (#4325337)
So because you can't think of an alternative, there should be no way of handling a scenario where, say, maybe that port is already in use?

Again, the explicitness is by design. My "handle" code would be checking the failure and acting on it: trying another port, logging a message, sending an email telling me my server isn't starting.

I doubt the existence of a real world app that really fires up an HTTP server and blindly dies without so much as logging to indicate failure.

Re:Quick and Clean in Go by masklinn on Thursday August 02, 2012 @01:05AM (#4327369)
> So because you can't think of an alternative, there should be no way of handling a scenario

And more strawmanning, great. Of course there is a way to handle failure: catch the bloody exception. Which is completely unlike what the original code does, hence your original comment being a (not very original) strawman.