
How to use interfaces in Go (2012) - abeinstein
http://jordanorelli.com/post/32665860244/how-to-use-interfaces-in-go
======
ridiculous_fish
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?

~~~
mseepgood
Use the Go oracle with an "implements" query.

~~~
namelezz
So beside familiarizing the language syntax, I need to be aware of a set of
unofficial tools and plugins, which are likely going to change in the next
couple months, to be able to read the code.

~~~
icholy
It's an official tool. Also, most interfaces are very small (one to two
methods) so it's pretty easy to figure out. I read/write a decent amount of Go
every day and it's not often that I'm asking myself "what implements this
interface?".

------
EdwardDiego
> type JavaProgrammer struct { > }

> func (j JavaProgrammer) Speak() string { > return "Design patterns!" > }

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

~~~
bassislife
> 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

------
dtft
Read this when I was first getting into Go; super helpful and a fun(ish) read
for people curious about the language

------
jdkanani
Interesting. For pointers and interfaces, in this sample
[http://play.golang.org/p/Lf8nn_pcYO](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.

~~~
ainar-g
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](https://golang.org/ref/spec#Calls)

------
nindalf
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.

------
programd
Previously on HN:

[https://news.ycombinator.com/item?id=9320235](https://news.ycombinator.com/item?id=9320235)

The blog post is from 2012.

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

~~~
krat0sprakhar
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.

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

------
slmyers
I don't understand why you would share a three year old post explaining the
basics of interfaces in Go.

