Skip to content

Instantly share code, notes, and snippets.

@oofnivek
Created March 7, 2022 07:47
Show Gist options
  • Save oofnivek/07dd04e1231c518698a1d41b7f372f90 to your computer and use it in GitHub Desktop.
Save oofnivek/07dd04e1231c518698a1d41b7f372f90 to your computer and use it in GitHub Desktop.
package main
import(
"fmt"
"reflect"
)
type Cat struct{}
func (c Cat) Say() string{return "meow"}
type Dog struct{}
func (d Dog) Say() string{return "woof"}
type Horse struct{}
func (h Horse) Say() string{return "neigh"}
func main(){
c:=Cat{}
d:=Dog{}
//initialize them into an array
animals:=[]Sayer{c,d}
//append horse into array
animals=append(animals,Horse{})
for _,a:= range animals{
MakeTalk(a)
}
}
type Sayer interface{
Say() string
}
func MakeTalk(s Sayer){
fmt.Println(reflect.TypeOf(s).Name(), "says:", s.Say())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment