package main
import (
"fmt"
"reflect"
)
type GoRute struct {
}
func (gr *GoRute) Test(a, b string) (string, error) {
return a + " " + b, nil
}
func (gr *GoRute) Demo(a, b string) (string, error) {
return a + " DDM " + b, nil
}
func main() {
RuteRun("GoRute", "Test")
RuteRun("GoRute", "Demo")
}
func RuteRun(ctl, mtd string) {
switch ctl {
case "GoRute":
gr := &GoRute{}
fmt.Println(gr)
default:
panic("unpick")
}
//gr := &GoRute{} //取消注释可以运行
rf := reflect.ValueOf(gr).MethodByName(mtd)
in := make([]reflect.Value, rf.Type().NumIn())
in[0] = reflect.ValueOf("OK")
in[1] = reflect.ValueOf("DO")
fmt.Println(rf.Call(in))
}
错误信息:
# command-line-arguments
.\test.go:33: undefined: gr
有疑问加站长微信联系(非本文作者)