github.com/switchupcb/yaegi@v0.10.2/_test/method33.go (about) 1 package main 2 3 import ( 4 "fmt" 5 ) 6 7 type T1 struct{} 8 9 func (t1 T1) f() { 10 fmt.Println("T1.f()") 11 } 12 13 func (t1 T1) g() { 14 fmt.Println("T1.g()") 15 } 16 17 type T2 struct { 18 T1 19 } 20 21 func (t2 T2) f() { 22 fmt.Println("T2.f()") 23 } 24 25 type I interface { 26 f() 27 } 28 29 func printType(i I) { 30 if t1, ok := i.(T1); ok { 31 println("T1 ok") 32 t1.f() 33 t1.g() 34 } 35 36 if t2, ok := i.(T2); ok { 37 println("T2 ok") 38 t2.f() 39 t2.g() 40 } 41 } 42 43 func main() { 44 println("T1") 45 printType(T1{}) 46 println("T2") 47 printType(T2{}) 48 } 49 50 // Output: 51 // T1 52 // T1 ok 53 // T1.f() 54 // T1.g() 55 // T2 56 // T2 ok 57 // T2.f() 58 // T1.g()