详解Golang利用反射reflect动态调用方法

2020-01-28 13:38:18丽君

编程语言中反射的概念

在计算机科学领域,反射是指一类应用,它们能够自描述和自控制。也就是说,这类应用通过采用某种机制来实现对自己行为的描述(self-representation)和监测(examination),并能根据自身行为的状态和结果,调整或修改应用所描述行为的状态和相关的语义。

每种语言的反射模型都不同,并且有些语言根本不支持反射。Golang语言实现了反射,反射机制就是在运行时动态的调用对象的方法和属性,官方自带的reflect包就是反射相关的,只要包含这个包就可以使用。

多插一句,Golang的gRPC也是通过反射实现的。

Golang的官方包 reflect 实现了运行时反射(run-time reflection)。运用得当,可谓威力无穷。今天,我们就来利用reflect进行方法的动态调用……

基本知识

首先,反射主要与 golang 的 interface 类型相关。一个 interface 类型的变量包含了两个指针:一个指向变量的类型,另一个指向变量的值。最常用的莫过于这两个函数:


func main(){
 s := "hello world"
 fmt.Println(reflect.ValueOf(s))  // hello world
 fmt.Println(reflect.TypeOf(s))  // string
}

其中,

reflect.ValueOf() 返回值类型:reflect.Value reflect.TypeOf() 返回值类型:reflect.Type

创建变量

接下来,我们可以使用 reflect  来动态的创建变量:


func main(){
 var s string
 t := reflect.TypeOf(s)
 fmt.Println(t)         // string
 sptr := reflect.New(t)
 fmt.Printf("%sn", sptr)    // %!s(*string=0xc00000e1e0)
}

需要留意, reflect.New() 返回的是一个 指针 :

New returns a Value representing a pointer to a new zero value for the specified type. That is, the returned Value's Type is PtrTo(typ).

这时候,我们可以使用 reflect.Value.Elem() 来取得其实际的值:


sval := sptr.Elem()  // 返回值类型:reflect.Value

然后再将其转为 interface 并做 type-assertion :


ss := sval.interface().(string)
fmt.Println(ss)    // 空字符串

动态调用

假设我们已经定义了以下的 struct 并实现了相关的方法:


type M struct{}
type In struct{}
type Out struct{}
 
func (m *M) Example(in In) Out {
 return Out{}
}

然后我们就可以通过下面这种方式来进行调用了:


func main() {
 v := reflect.ValueOf(&M{})
 m := v.MethodByName("Example")
 in := m.Type().In(0)
 out := m.Type().Out(0)
 fmt.Println(in, out)
    
 inVal := reflect.New(in).Elem()
    // 可以将 inVal 转为interface后进行赋值之类的操作……
 rtn := m.Call([]reflect.Value{inVal})
 fmt.Println(rtn[0])
}