go reflect 场景实践
动态调用函数(无参数)
type T struct {}func main() {
name := "Do"
t := &T{}
reflect.ValueOf(t).MethodByName(name).Call(nil)
}func (t *T) Do() {
fmt.Println("hello")
}
动态调用函数(有参数)
type T struct{}func main() {
name := "Do"
t := &T{}
a := reflect.ValueOf(1111)
b := reflect.ValueOf("world")
in := []reflect.Value{a, b}
reflect.ValueOf(t).MethodByName(name).Call(in)
}func (t *T) Do(a int, b string) {
fmt.Println("hello" + b, a)
}