編程語言中反射的概念
在計算機科學領域,反射是指一類應用,它們能夠自描述和自控制。也就是說,這類應用通過采用某種機制來實現對自己行為的描述(self-representation)和監測(examination),并能根據自身行為的狀態和結果,調整或修改應用所描述行為的狀態和相關的語義。
每種語言的反射模型都不同,并且有些語言根本不支持反射。Golang語言實現了反射,反射機制就是在運行時動態的調用對象的方法和屬性,官方自帶的reflect包就是反射相關的,只要包含這個包就可以使用。
多插一句,Golang的gRPC也是通過反射實現的。
Golang的官方包 reflect 實現了運行時反射(run-time reflection)。運用得當,可謂威力無窮。今天,我們就來利用reflect進行方法的動態調用……
基本知識
首先,反射主要與 golang 的 interface 類型相關。一個 interface 類型的變量包含了兩個指針:一個指向變量的類型,另一個指向變量的值。最常用的莫過于這兩個函數:
1
2
3
4
5
|
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 來動態的創建變量:
1
2
3
4
5
6
7
|
func main(){ var s string t := reflect.TypeOf(s) fmt.Println(t) // string sptr := reflect.New(t) fmt.Printf("%s\n", 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() 來取得其實際的值:
1
|
sval := sptr.Elem() // 返回值類型:reflect.Value |
然后再將其轉為 interface 并做 type-assertion :
1
2
|
ss := sval.interface().(string) fmt.Println(ss) // 空字符串 |
動態調用
假設我們已經定義了以下的 struct 并實現了相關的方法:
1
2
3
4
5
6
7
|
type M struct{} type In struct{} type Out struct{} func (m *M) Example(in In) Out { return Out{} } |
然后我們就可以通過下面這種方式來進行調用了:
1
2
3
4
5
6
7
8
9
10
11
12
|
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]) } |
注冊方法
我們再定義一個保存 M 所有方法的 map struct :
1
2
3
4
5
6
7
|
type Handler struct { Func reflect.Value In reflect.Type NumIn int Out reflect.Type NumOut int } |
然后我們就可以來遍歷結構體 M 的所有方法了:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
func main() { handlers := make(map[string]*Handler) v := reflect.ValueOf(&M{}) t := reflect.TypeOf(&M{}) for i := 0; i < v.NumMethod(); i++ { name := t.Method(i).Name // 可以根據 i 來獲取實例的方法,也可以用 v.MethodByName(name) 獲取 m := v.Method(i) // 這個例子我們只獲取第一個輸入參數和第一個返回參數 in := m.Type().In(0) out := m.Type().Out(0) handlers[name] = &Handler{ Func: m, In: in, NumIn: m.Type().NumIn(), Out: out, NumOut: m.Type().NumOut(), } } } |
Elem()
在學習 reflect 的過程中,我們發現 reflect.Value 和 reflect.Type 都提供了 Elem() 方法。
reflect.Value.Elem() 的作用已經在前面稍微提到了,主要就是返回一個 interface 或者 pointer 的值:
Elem returns the value that the interface v contains or that the pointer v points to. It panics if v's Kind is not Interface or Ptr. It returns the zero Value if v is nil.
reflect.Type.Elem() 的作用則是返回一個類型(如:Array,Map,Chan等)的元素的類型:
Elem returns a type's element type. It panics if the type's Kind is not Array, Chan, Map, Ptr, or Slice.
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://chenjiehua.me/golang/golang-call-method-by-reflection.html