package main

import (
    "fmt"
    "reflect"
)

type A struct {
}

func (A) Test() {
    fmt.Println("gooooo reflect call empty param method")
}

func (A) TestwithParam(ap string, bp string) {
    fmt.Println("gooooo reflect call with Param method:", ap, bp)
}

func main() {

    var a A
    f := reflect.ValueOf(a)
    //空参数时方法调用 一 序号
    f.Method(0).Call(nil) //gooooo method

    //空参数时方法调用 二 方法名
    in := make([]reflect.Value, 0)
    f.MethodByName("Test").Call(in) //gooooo method

    //多参数调用
    parray := []string{"hello", "reflect"}
    inp := make([]reflect.Value, len(parray))

    for k, param := range parray {
        inp[k] = reflect.ValueOf(param)
    }

    f.MethodByName("TestwithParam").Call(inp)
}

//打印
//gooooo reflect call empty param method
//gooooo reflect call empty param method
//gooooo reflect call with Param method: hello reflect

 

相关文章:

  • 2022-12-23
  • 2021-04-28
  • 2022-12-23
  • 2021-06-07
  • 2022-12-23
  • 2021-07-20
  • 2022-12-23
猜你喜欢
  • 2022-01-22
  • 2021-11-21
  • 2021-07-26
  • 2022-12-23
  • 2021-08-06
  • 2021-05-21
  • 2022-01-11
相关资源
相似解决方案