github.com/lsg2020/gort@v0.0.0-20220515072951-7a7794baa036/examples/hello/hello.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "log" 6 "reflect" 7 8 "github.com/lsg2020/gort" 9 ) 10 11 type testStruct struct { 12 str string 13 } 14 15 func (p *testStruct) Print(format string, args ...interface{}) { 16 log.Printf("in testStruct.Print %#v\n", p) 17 log.Printf(format, args...) 18 } 19 20 var testGlobal = testStruct{ 21 str: "test global", 22 } 23 24 func main() { 25 log.Printf("%#v\n", testGlobal) 26 27 rt, err := gort.NewDwarfRT("") 28 if err != nil { 29 log.Fatalf("load dwarf err %s\n", err) 30 return 31 } 32 33 // test find type 34 typ, err := rt.FindType("main.testStruct") 35 if err != nil { 36 log.Fatalf("load type err %s\n", err) 37 return 38 } 39 rTest := reflect.New(typ) 40 log.Printf("new main.testStruct %#v", rTest.Interface()) 41 42 // test global 43 rGlobal, err := rt.FindGlobal("main.testGlobal") 44 if err != nil { 45 log.Fatalf("load global err %s\n", err) 46 return 47 } 48 log.Printf("load main.testGlobal %#v", rGlobal.Interface()) 49 50 // test func 51 fmt.Printf("test call fmt.Printf\n") 52 53 _, err = rt.CallFunc("fmt.Printf", true, []reflect.Value{ 54 reflect.ValueOf("test call fmt.Printf:%d %s\n"), 55 reflect.ValueOf(1234), 56 reflect.ValueOf("hello"), 57 }) 58 if err != nil { 59 log.Fatalf("test call err %s\n", err) 60 return 61 } 62 63 _, err = rt.CallFunc("main.(*testStruct).Print", true, []reflect.Value{ 64 reflect.ValueOf(&testGlobal), 65 reflect.ValueOf("test call method:%d %s\n"), 66 reflect.ValueOf(1234), 67 reflect.ValueOf("hello"), 68 }) 69 if err != nil { 70 log.Fatalf("test call err %s\n", err) 71 return 72 } 73 74 }