Aspire

go reflect 场景实践

发布时间:6年前热度: 4470 ℃评论数:

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)
}


源码安装、技术讨论、二次开发等联系微信:

reflect 场景实践

栏目导航

  1. PHP
  2. Go
  3. Mysql
  4. Linux
  5. 前端
  6. 杂谈

手机扫码访问