Created
March 7, 2022 07:47
-
-
Save oofnivek/07dd04e1231c518698a1d41b7f372f90 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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