Golang Interface

Kevin FOO
1 min readMar 7, 2022

I personally find that explanation and example given at Golang Tour (go.dev/tour) is really confusing. I had to google around and was lucky enough to find an article at stack overflow with a better explanation.

I worry that I might not be able to find that article again on my next search when I need it and it is best that I jot it down as my own notes for future references. In layman terms I would say that it is a way to group all methods of the same name.

In Golang, method is a function with a receiver argument. (https://go.dev/tour/methods/2)

From the example, you can see that Cat and Dog has the same method Say. It looks like duplicate codes of copy and pasting with minor changes. Wouldn’t it be nicer if I were to put them in a for loop?

As you can see from the above modified code, there is an array named animals with a for loop that loops through it and calls the method Say.

< Back to all the stories I had written

--

--

Kevin FOO

A software engineer, a rock climbing, inline skating enthusiast, a husband, a father.