Live data from Hacker News

When in Go, do as Gophers do

talks.golang.org

31–40 of 93 posts

Re: When in Go, do as Gophers do

#31
post #26

Earlier quoted context omitted.

I looked at this slide for a while and couldn't understand it. Could you explain the detail a bit ? It seems like relying on dynamic type casting over a nil variable, is that correct ? But then what would the following "if" look like ?

This is how you can make sure that a struct implements an interface. The line 'var _ scan.Writer = (*ColumnWriter)(nil)' assigns a nil pointer of the struct to a scan.Writer interface reference which would fail if ColumnWriter doesn't implement the interface. And the casting always works.

So it's a static typing check ? the code won't even compile if the ColumnWriter struct doesn't implement the interface ?

Re: When in Go, do as Gophers do

#32
post #7

Sad that this is still the way to test for interface implementations. http://talks.golang.org/2014/readability.slide#17

It is a perfectly reasonable way to test for interfaces. A single struct may satisfy a hundred interfaces which it doesn't even know about, this is actually one of the best traits of go.

No, it is a workaround pattern due to lack of language support.

Re: When in Go, do as Gophers do

#33
post #21

Earlier quoted context omitted.

If you have only worked in languages with exceptions, it's not surprising that you would struggle with Go's C-style error handling. I've grown to prefer it, as exception handling typically boils down to "not my problem" in most code bases. Go forces you to think through each error condition, which is an unusual amount of effort for people who may not be accustomed to it.

Go's error handling works well in small programs but there's a couple of issues. One is that it quickly becomes hard to find out where errors really came from. Consider this idiomatic code: func MyFunc(input int) (output int, err error) { if output, err = someOtherFuncA(input); err != nil { return } if output, err = someOtherFuncB(output); err != nil { return } // ... } If MyFunc returns an error it's not possible to…

That's not really idiomatic. You should be adding context to the error you return, or a new error.

Re: When in Go, do as Gophers do

#34
post #28

As I go from normal Go style to idiomatic Go style, I find more error checks. That is annoying when your code grows. http://talks.golang.org/2014/readability.slide#11 In this example, have to check error 3 times to write 4 lines of code.

In Java, I'd have to write 3 catch blocks. It's possible to write a blanket catch, toss it up to higher levels and make it someone else's problem. I vaguely recall this being against Pike's vision of what "exceptional" means and he wanted a language that forces developers to think of disk/network failure as a very common case.

Given that every single error check there is just passing up the error, is it really that different from having exceptions that propagate along with RAII style resource cleanup? The vast vast vast majority of Go code simply propagates errors.

Re: When in Go, do as Gophers do

#35
post #22

Earlier quoted context omitted.

That's a question i've often asked myself, whether having me think about error handling at almost every line of code was actually a good thing, or just plain overkill since most of time killing the top-most function ( and maybe retry it) is the only thing to do. I don't really have an answer to that. But what i know is that i find the practice of having typed exception in java plus catching the one you want to deal w…

I agree with that as well. I was just now writing a program to receive a command and perform an action, and sometimes it will receive invalid commands, or there will be some error performing the action. In these cases, I just want it to ignore the errors and resume its loop. With Python, you just wrap it in a try/catch and forget about it. With Go, I already wrote five error checks and I'm sure I haven't caught some…

I don't understand. What's wrong with this in the loop?

    if err != nil {
        continue
    }

Re: When in Go, do as Gophers do

#36
post #13

Earlier quoted context omitted.

> I've never known a language with so much discussion on why what you are doing isn't idiomatic as Go. Python is another language where there is a lot of talk about idiomatic code ('Pythonic'). C++11 and C++14 also have a lot of idiomatic discussion since the community's trying to move to a different style with the latest enhancements. Not sure if this is a signal of core issues.

We Ruby programmers also obsess over style, especially with regards to tests. There's even a full, printed book about it: "Eloquent Ruby."

An exceptionally well written book, I'd like to add.

Many authors try to unite casual style with technical writing. Russ Olsen does it beautifully.

He never treats you as a child or an idiot. He never lets you feel his master status. He's like the good-natured old-timer who sits next to you with a cup of coffee, teaching you more than you realize.

Re: When in Go, do as Gophers do

#37
post #13
post #2

I've never known a language with so much discussion on why what you are doing isn't idiomatic as Go. I'm enjoying using Go for the few small services I'm using it for but it seems that a language which has to constantly fight it's users to reinforce what it considers idiomatic has some core issues.

> I've never known a language with so much discussion on why what you are doing isn't idiomatic as Go. Python is another language where there is a lot of talk about idiomatic code ('Pythonic'). C++11 and C++14 also have a lot of idiomatic discussion since the community's trying to move to a different style with the latest enhancements. Not sure if this is a signal of core issues.

Python is another language where there is a lot of talk about idiomatic code ('Pythonic').

I wonder what an appropriate adjective could be for idiomatic Go code. Gonic? Gonian?

Re: When in Go, do as Gophers do

#38
post #30

I've only been using Go for less than a week and my impressions are less than good. I'm still waiting and hoping to understand what it is that people like about Go, but I'm starting to suspect that won't happen. The biggest problem I have had most so far is when reading other code, finding the meaning amongst all of the error nil checks and the sprawling if conditions that they bring. Another thing that makes it diff…

That is the way it is done in C: avoid indentation if you can fail out. It is very readable.

Re: When in Go, do as Gophers do

#39
post #35

Earlier quoted context omitted.

I agree with that as well. I was just now writing a program to receive a command and perform an action, and sometimes it will receive invalid commands, or there will be some error performing the action. In these cases, I just want it to ignore the errors and resume its loop. With Python, you just wrap it in a try/catch and forget about it. With Go, I already wrote five error checks and I'm sure I haven't caught some…

I don't understand. What's wrong with this in the loop? if err != nil { continue }

What's wrong is that it's not this:

Re: When in Go, do as Gophers do

#40
post #14

Earlier quoted context omitted.

Go's error handling remains verbose, unsafe and error-prone compared to more modern languages which use return values for error signaling (Erlang, MLs, Haskell, Rust). The problem is not that Go's error handling requires more thinking or efforts, it's that it is bad , and while that's an improvement from C's terrible error handling it's still nowhere near good enough, let alone good (unless you consider C's error han…

Feel free to enlighten us specifically about how they are better.

In Go, you're not returning a sum type, so it's down to the programmer to remember that the "real" return value is likely toxic waste when its copassenger the error return value is non-nil.
Post reply on HN