https://jordanorelli.com/post/32665860244/how-to-use-interfaces-in-go
Interfaces allow us to define a type by what it can do rather than what it is.
type Animal interface {
Speak() string
}
In this example and Animal is anything that can Speak(). Here's some types
that satisfy this interface.
type Dog struct {
}
func (d Dog) Speak() string {
return "Woof!"
}
type Cat struct {
}
func (c Cat) Speak() string {
return "Meow!"
}
type Llama struct {
}
func (l Llama) Speak() string {
return "?????"
}
Now we could throw these in a slice and see what each animal says:
func main() {
animals := []Animal{Dog{}, Cat{}, Llama{}, JavaProgrammer{}}
for _, animal := range animals {
fmt.Println(animal.Speak())
}
}
This article describes an idiomatic approach to using channels inside
interfaces:
https://kdsch.org/post/clean-concurrency-go/