github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/gnovm/tests/backup/assert1.gno (about) 1 package main 2 3 import ( 4 "fmt" 5 "reflect" 6 "time" 7 ) 8 9 type TestStruct struct{} 10 11 func (t TestStruct) String() string { 12 return "hello world" 13 } 14 15 func main() { 16 aType := reflect.TypeOf((*fmt.Stringer)(nil)).Elem() 17 18 var t interface{} 19 t = time.Nanosecond 20 s, ok := t.(fmt.Stringer) 21 if !ok { 22 fmt.Println("time.Nanosecond does not implement fmt.Stringer") 23 return 24 } 25 fmt.Println(s.String()) 26 fmt.Println(t.(fmt.Stringer).String()) 27 bType := reflect.TypeOf(time.Nanosecond) 28 fmt.Println(aType.IsImplementedBy(bType)) 29 30 t = 42 31 foo, ok := t.(fmt.Stringer) 32 if !ok { 33 fmt.Println("42 does not implement fmt.Stringer") 34 } else { 35 fmt.Println("42 implements fmt.Stringer") 36 } 37 _ = foo 38 39 var tt interface{} 40 tt = TestStruct{} 41 ss, ok := tt.(fmt.Stringer) 42 if !ok { 43 fmt.Println("TestStuct does not implement fmt.Stringer") 44 return 45 } 46 fmt.Println(ss.String()) 47 fmt.Println(tt.(fmt.Stringer).String()) 48 // TODO(mpl): uncomment when fixed 49 // cType := reflect.TypeOf(TestStruct{}) 50 // fmt.Println(aType.IsImplementedBy(cType)) 51 } 52 53 // Output: 54 // 1ns 55 // 1ns 56 // true 57 // 42 does not implement fmt.Stringer 58 // hello world 59 // hello world