1 package main 2 3 import ( 4 "fmt" 5 ) 6 7 //define a interface 8 type ITest interface { 9 myTest() 10 } 11 12 //implement 13 type Man struct{} 14 15 func (man *Man) myTest() { 16 fmt.Print("man\n") 17 } 18 19 //implement 20 type Woman struct{} 21 22 func (woman *Woman) myTest() { 23 fmt.Print("woman\n") 24 } 25 26 func testFunction(test ITest) { 27 test.myTest() 28 } 29 30 func main() { 31 32 var man, woman ITest 33 34 man = &Man{} 35 woman = &Woman{} 36 37 testFunction(man) 38 testFunction(woman) 39 }
有疑问加站长微信联系(非本文作者)