News for hackers. Stuff that matters.
Re:Things about programming I learned with Go by _ph_ on Tuesday August 22, 2017 @10:16AM (#15072607)
I am not aware that they did. Most good Go code does not do this. That there exist some badly written Go programs does not contradict this. Go functions that can fail, should return an error as part of the function returns. Like with exceptions, you can decide to ignore any error or properly handle it. Even with checked exceptions, you can have an empty "catch", which means you are just ignoring it. So the difference is rather syntactical. And like with exceptions, proper error handling is entirely the responsibility of the programmer who writes the error handling code. The Go version of error handling requires less typing overall, and makes it a little bit easier to ignore errors. Unhanded exceptions on the other side can unwind the stack arbitrarily high up until they do eventually find a handler - but it is not said, that this handler can properly handle the exception, as it might be separated to far from the error source.
Re:Things about programming I learned with Go by erikb on Tuesday August 22, 2017 @05:09PM (#15076416)
As far as I know bubbling up errors instead of handling them is recommended practice: https://github.com/nats-io/go-nats-streaming/issues/143#issu...
Re:Things about programming I learned with Go by vram22 on Tuesday August 22, 2017 @11:43AM (#15073497)
>The Go version of error handling requires less typing overall

Do you mean as compared to using exceptions in a language like Python or Java? If so, can you give an example of code that does the same task, in both types of languages, to make this more clear? Thanks.

Re:Things about programming I learned with Go by _ph_ on Tuesday August 22, 2017 @03:35PM (#15075657)
I was mostly thinking of Java. Ignoring an exception would be done with try {....} catch (Exception e) {}, while in Go, you would do: x,_ := f(...). And if you want to check a returned error I think the if err!=nil {...} is still a bit less typing than the Java version, and you don't have to declare checked exceptions you might throw.
Re:Things about programming I learned with Go by concede_pluto on Wednesday August 23, 2017 @10:35PM (#15087186)
In Go, if you call three functions you need to repeat the

  x, err := do_something()
  if err != nil {
          return nil, err
  }
ceremony three times. In Java it happens by default, because the language designers agreed this is by far the most common case.

Ignoring an error is almost always a serious mistake, so the fact that Go makes it easy and not blatant is not a good thing.

Re:Things about programming I learned with Go by _ph_ on Friday August 25, 2017 @10:52AM (#15098849)
Yes, I certainly was not advocating ignoring any error. Which is, why exceptions are of dubious use. If you only check exceptions around a larger block of function calls, or every several levels of the call stack, then you might miss the exact source of the exception, unless the exception type is unambiguous. That is, why I find the Go version tedious for sure, but still better than to wrap single function calls into try... catch.
Re:Things about programming I learned with Go by vram22 on Thursday August 24, 2017 @07:09AM (#15089109)
>ceremony three times. In Java it happens by default,

What happens by default? Not clear. (I know Java, but not up to date with recent versions.)

>Ignoring an error is almost always a serious mistake

Agreed.

Re:Things about programming I learned with Go by concede_pluto on Thursday August 24, 2017 @01:37PM (#15092206)
In Java, an exception outside a catch block automatically stops the method and raises the exception to the caller, and then their caller, and so on. In Go you have to write this after every function call (except the tiny minority that can only panic).
Re:Things about programming I learned with Go by vram22 on Saturday August 26, 2017 @05:28PM (#15107571)
Thanks. I did know that about Java (having used it), just that it was not clear to me from your earlier wording, that that is what you were saying. NP.
Re:Things about programming I learned with Go by vram22 on Tuesday August 22, 2017 @03:47PM (#15075736)
Got it now.