Live data from Hacker News

How to use interfaces in Go (2012)

jordanorelli.com

1–10 of 30 posts

Re: How to use interfaces in Go (2012)

#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?

Re: How to use interfaces in Go (2012)

#3
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?

I think the syntax is terse mostly for the sake of example.

There's a lot going on in about 3 lines.

Re: How to use interfaces in Go (2012)

#7
The article is a pretty good intro to how to use interfaces. I think some people, especially those not familiar with the language would benefit from reading such articles and learning how to write idiomatic Go code, rather than ask for features that would allow them to write idiomatic $LANGUAGE_THEY_LIKE code.

Re: How to use interfaces in Go (2012)

#8
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?

This is a very contrived example to illustrate what an interface is in Go. You will almost likely never see code written like that in a good project.

Re: How to use interfaces in Go (2012)

#9
post #3
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?

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 tell it is a horribly grotesque language considering when it was created. Sure, even C or C++ have curly braces and array specifiers for types; however, at least these have the excuse of age and compatibility to work with. Go is relatively new, and I think that welder was getting at is that syntax shouldn't be some overly complex thing in this day and age. Nim and Elixir come to mind as two (newer) languages that avoid many syntax warts without making significant trade-offs because of it.
Post reply on HN