Live data from Hacker News

How to use interfaces in Go (2012)

jordanorelli.com

11–20 of 30 posts

Re: How to use interfaces in Go (2012)

#11
post #2

> func main() { > animals := []Animal{Dog{}, Cat{}, Llama{}, JavaProgrammer{}} > for _, animal := range animals { > fmt.Println(animal.Speak()) > } > } This makes my eyes hurt. In this modern age we still haven't learned to create easy to read languages?

Not sure I understand - what part of this is not easy to read? It's quite familiar to read and make sense to any programmer who's had experience with any of the C-like languages.

Re: How to use interfaces in Go (2012)

#12

> type JavaProgrammer struct { > } > func (j JavaProgrammer) Speak() string { > return "Design patterns!" > } shrug At least our language designers trusted our intelligence enough to implement generics.

> shrug At least our language designers trusted our intelligence enough to implement generics.

Trusted you so much that they even tripped on co/contravariance to add to the challenge :p

Re: How to use interfaces in Go (2012)

#14
When navigating unfamiliar Go codebases, it's hard to know which types fulfill an interface. Say you have a function that takes an Animal; how do you figure out what to call it with?

Or to give a concrete example, say you want to do formatted output to stderr. How is one supposed to determine that os.Stderr is something that can be passed to fmt.Fprintf? Is there a better way than manually comparing method signatures?

Re: How to use interfaces in Go (2012)

#15
post #2

> func main() { > animals := []Animal{Dog{}, Cat{}, Llama{}, JavaProgrammer{}} > for _, animal := range animals { > fmt.Println(animal.Speak()) > } > } This makes my eyes hurt. In this modern age we still haven't learned to create easy to read languages?

Not sure I understand - what part of this is not easy to read? It's quite familiar to read and make sense to any programmer who's had experience with any of the C-like languages.

Is this sarcasm? That code snippet doesn't look like any C-like language that I'm familiar with.

Re: How to use interfaces in Go (2012)

#16

Interesting. For pointers and interfaces, in this sample http://play.golang.org/p/Lf8nn_pcYO , `h3.Add2()` should fail as per OP said. I am sure, I am missing something important here. Please help.

It shouldn't. The method you're calling takes a pointer receiver, and the Go Spec[1] says:

>A method call x.m() is valid if the method set of (the type of) x contains m and the argument list can be assigned to the parameter list of m. If x is addressable and &x's method set contains m, x.m() is shorthand for(&x).m()

In other words, if a type T no method M, it will look for M in *T's methods.

[1] https://golang.org/ref/spec#Calls

Re: How to use interfaces in Go (2012)

#17
post #3

Earlier quoted context omitted.

I think the syntax is terse mostly for the sake of example. There's a lot going on in about 3 lines.

I don't think "terse" quite describes it. Python can be terse, as you can say: n = sum(x for x in my_sequence if x % 2 == 0) But the Go code listed above is not just plagued with a lot going on in "3 lines". I think there's something to be said about gratuitous operators, and a sort of beauty to letting the parser do more work than the programmer themselves. I don't use Go at all, but from the example above I can tel…

> I don't use Go at all, but from the example above I can tell it is a horribly grotesque language...

If you don't us Go at all then you can't tell anything from a single 3 line example. It's like judging an entire Linux distro based solely on a couple of screenshots.

What the he'll is going wrong with HN lately that I can't open a programming thread without superficial criticisms from someone who's never actually coded in that particular language, and troll posts about how Java / whatever is better / already supports feature x.....

People complain about the zealous attitudes of language fanboys, but honestly I think the dogmatic negativity on HN is becoming a far greater issue.

Re: How to use interfaces in Go (2012)

#18

When navigating unfamiliar Go codebases, it's hard to know which types fulfill an interface. Say you have a function that takes an Animal; how do you figure out what to call it with? Or to give a concrete example, say you want to do formatted output to stderr. How is one supposed to determine that os.Stderr is something that can be passed to fmt.Fprintf? Is there a better way than manually comparing method signatures…

Use the Go oracle with an "implements" query.

Re: How to use interfaces in Go (2012)

#19
post #2

> func main() { > animals := []Animal{Dog{}, Cat{}, Llama{}, JavaProgrammer{}} > for _, animal := range animals { > fmt.Println(animal.Speak()) > } > } This makes my eyes hurt. In this modern age we still haven't learned to create easy to read languages?

On top of that, the code above ignores errors, something that Go makes all too easy by not supporting exceptions.

Re: How to use interfaces in Go (2012)

#20
post #3

Earlier quoted context omitted.

I think the syntax is terse mostly for the sake of example. There's a lot going on in about 3 lines.

I don't think "terse" quite describes it. Python can be terse, as you can say: n = sum(x for x in my_sequence if x % 2 == 0) But the Go code listed above is not just plagued with a lot going on in "3 lines". I think there's something to be said about gratuitous operators, and a sort of beauty to letting the parser do more work than the programmer themselves. I don't use Go at all, but from the example above I can tel…

And Python itself is pretty verbose compared to modern languages:

    mySequence.filter { it % 2 == 0 }.sum()
Post reply on HN