Golang Interfaces
In this post I will describe briefly how Golang interfaces work.
The interface
The main idea we for this sample is: an animal, a Dog and a Cat that behaves as an animal.
Here, we are defining which functions are required to a type to work as a Animal:
package abstraction
import "fmt"
type Animal interface {
Talk() string
}
The Dog
In order to Dog to work as an Animal, the only thing you have to do is have the function with the same name (Talk), with the same parameters:
type Dog struct {
name string
}
func (dog Dog) Talk() string{
return "woof woof"
}
The Cat
The same for a cat:
type Cat struct{
name string
}
func (cat Cat) Talk() string{
return "meow"
}
The test
Lets instantiate a lot of Dogs and Cats, and see if Go allows us to use Dogs and Cats as Animal:
func createAnimals() []Animal{
return []Animal{ Dog{"snoopy"}, Cat{"mousty"}, Dog{"rex"}, Cat{"garfield"}, Cat{"fat garfield"}, Dog{"toto"} }
}
func MakeAnimalsTalk(){
for _, animal := range createAnimals(){
fmt.Println(animal.Talk())
}
}
The test 2
package abstraction
import "testing"
func TestMakeAnimalsTalk(t *testing.T) {
MakeAnimalsTalk()
}
The output
/home/mussatto/dev/go-1.7/bin/go test -v github.com/mussatto/golab/abstraction -run ^TestMakeAnimalsTalk$
woof woof
meow
woof woof
meow
meow
woof woof