import"fmt" type myInF interface{ SomeFunc() int }//接口声明 type myInt int func(i myInt) SomeFunc() int{ returnint(i) }//接口实现
type myString string func(str myString) SomeFunc() int{ returnlen(str) }
type myStruct struct{ myInt }
funcmain(){ var inf myInF var i myInt = 100 inf = i //接口与实际方法关联 fmt.Println(inf.SomeFunc()) var str myString = "100" inf = str fmt.Println(str.SomeFunc()) //多态 stu := myStruct{i} //与结构体相结合 inf = stu fmt.Println(inf.SomeFunc()) }