Golang反射深入理解
基本瞭解
在Go語言中,大多數時候值/型別/函式非常直接,要的話,定義一個。你想要個Struct
type Foo struct { A int B string }
你想要一個值,你定義出來
var x Foo
你想要一個函式,你定義出來
func DoSomething(f Foo) { fmt.Println(f.A, f.B) }
但是有些時候,你需要搞一些執行時才能確定的東西,比如你要從檔案或者網路中獲取一些字典資料。又或者你要搞一些不同型別的資料。在這種情況下,reflection
就有用啦。reflection能夠讓你擁有以下能力
- 在執行時檢查type
-
在執行時檢查/修改/建立 值/函式/結構
總的來說,go的reflection
圍繞者三個概念Types
,Kinds
,Values
。 所有關於反射的操作都在reflect
包裡面
反射的Power
Type的Power
首先,我們看看如何通過反射來獲取值得型別。
varType := reflect.TypeOf(var)
從反射介面可以看到有一大堆得函式等著我們去用。可以從註釋裡面看到。反射包預設我們知道我們要幹啥子,比如varType.Elem()
就會panic
。因為Elem()只有Array, Chan, Map, Ptr, or Slice.
這些型別才有這個方法。具體可以檢視測試程式碼。通過執行以下程式碼可檢視所有reflect函式的示例
package main import ( "fmt" "reflect" ) type FooIF interface { DoSomething() DoSomethingWithArg(a string) DoSomethingWithUnCertenArg(a ... string) } type Foo struct { A int B string C struct { C1 int } } func (f *Foo) DoSomething() { fmt.Println(f.A, f.B) } func (f *Foo) DoSomethingWithArg(a string) { fmt.Println(f.A, f.B, a) } func (f *Foo) DoSomethingWithUnCertenArg(a ... string) { fmt.Println(f.A, f.B, a[0]) } func (f *Foo) returnOneResult() int { return 2 } func main() { var simpleObj Foo var pointer2obj = &simpleObj var simpleIntArray = [3]int{1, 2, 3} var simpleMap = map[string]string{ "a": "b", } var simpleChan = make(chan int, 1) var x uint64 var y uint32 varType := reflect.TypeOf(simpleObj) varPointerType := reflect.TypeOf(pointer2obj) // 對齊之後要多少容量 fmt.Println("Align: ", varType.Align()) // 作為結構體的`field`要對其之後要多少容量 fmt.Println("FieldAlign: ", varType.FieldAlign()) // 叫啥 fmt.Println("Name: ", varType.Name()) // 絕對引入路徑 fmt.Println("PkgPath: ", varType.PkgPath()) // 實際上用了多少記憶體 fmt.Println("Size: ", varType.Size()) // 到底啥型別的 fmt.Println("Kind: ", varType.Kind()) // 有多少函式 fmt.Println("NumMethod: ", varPointerType.NumMethod()) // 通過名字獲取一個函式 m, success := varPointerType.MethodByName("DoSomethingWithArg") if success { m.Func.Call([]reflect.Value{ reflect.ValueOf(pointer2obj), reflect.ValueOf("sad"), }) } // 通過索引獲取函式 m = varPointerType.Method(1) m.Func.Call([]reflect.Value{ reflect.ValueOf(pointer2obj), reflect.ValueOf("sad2"), }) // 是否實現了某個介面 fmt.Println("Implements:", varPointerType.Implements(reflect.TypeOf((*FooIF)(nil)).Elem())) //看看指標多少bit fmt.Println("Bits: ", reflect.TypeOf(x).Bits()) // 檢視array, chan, map, ptr, slice的元素型別 fmt.Println("Elem: ", reflect.TypeOf(simpleIntArray).Elem().Kind()) // 檢視Array長度 fmt.Println("Len: ", reflect.TypeOf(simpleIntArray).Len()) // 檢視結構體field fmt.Println("Field", varType.Field(1)) // 檢視結構體field fmt.Println("FieldByIndex", varType.FieldByIndex([]int{2, 0})) // 檢視結構提field fi, success2 := varType.FieldByName("A") if success2 { fmt.Println("FieldByName", fi) } // 檢視結構體field fi, success2 = varType.FieldByNameFunc(func(fieldName string) bool { return fieldName == "A" }) if success2 { fmt.Println("FieldByName", fi) } //檢視結構體數量 fmt.Println("NumField", varType.NumField()) // 檢視map的key型別 fmt.Println("Key: ", reflect.TypeOf(simpleMap).Key().Name()) // 檢視函式有多少個引數 fmt.Println("NumIn: ", reflect.TypeOf(pointer2obj.DoSomethingWithUnCertenArg).NumIn()) // 檢視函式引數的型別 fmt.Println("In: ", reflect.TypeOf(pointer2obj.DoSomethingWithUnCertenArg).In(0)) // 檢視最後一個引數,是否解構了 fmt.Println("IsVariadic: ", reflect.TypeOf(pointer2obj.DoSomethingWithUnCertenArg).IsVariadic()) // 檢視函式有多少輸出 fmt.Println("NumOut: ", reflect.TypeOf(pointer2obj.DoSomethingWithUnCertenArg).NumOut()) // 檢視函式輸出的型別 fmt.Println("Out: ", reflect.TypeOf(pointer2obj.returnOneResult).Out(0)) // 檢視通道的方向, 3雙向。 fmt.Println("ChanDir: ", int(reflect.TypeOf(simpleChan).ChanDir())) // 檢視該型別是否可以比較。不能比較的slice, map, func fmt.Println("Comparable: ", varPointerType.Comparable()) // 檢視型別是否可以轉化成另外一種型別 fmt.Println("ConvertibleTo: ", varPointerType.ConvertibleTo(reflect.TypeOf("a"))) // 該型別的值是否可以另外一個型別 fmt.Println("AssignableTo: ", reflect.TypeOf(x).AssignableTo(reflect.TypeOf(y))) }
Value的Power
除了檢查變數的型別,你可以通過reflection
來讀/寫/新建一個值。不過首先先獲取反射值型別
refVal := reflect.ValueOf(var)
如果你想要修改變數的值。你需要獲取反射指向該變數的指標,具體原因後面解釋
refPtrVal := reflect.ValueOf(&var)
當然你有了reflect.Value
,通過Type()
方法可以很容易的獲取reflect.Type
。如果要改變該變數的值用
refPtrVal.Elem().Set(newRefValue)
當然Set
方法的引數必須也得是reflect.Value
如果你想建立一個新的值,用以下下程式碼
newPtrVal := reflect.New(varType)
然後在用Elem().Set()
來進行值的初始化。當然還有不同的value有一大堆的不同的方法。這裡就不寫了。我們重點看看下面這段官方例子
package main import ( "fmt" "reflect" ) func main() { // swap is the implementation passed to MakeFunc. // It must work in terms of reflect.Values so that it is possible // to write code without knowing beforehand what the types // will be. swap := func(in []reflect.Value) []reflect.Value { return []reflect.Value{in[1], in[0]} } // makeSwap expects fptr to be a pointer to a nil function. // It sets that pointer to a new function created with MakeFunc. // When the function is invoked, reflect turns the arguments // into Values, calls swap, and then turns swap's result slice // into the values returned by the new function. makeSwap := func(fptr interface{}) { // fptr is a pointer to a function. // Obtain the function value itself (likely nil) as a reflect.Value // so that we can query its type and then set the value. fn := reflect.ValueOf(fptr).Elem() // Make a function of the right type. v := reflect.MakeFunc(fn.Type(), swap) // Assign it to the value fn represents. fn.Set(v) } // Make and call a swap function for ints. var intSwap func(int, int) (int, int) makeSwap(&intSwap) fmt.Println(intSwap(0, 1)) // Make and call a swap function for float64s. var floatSwap func(float64, float64) (float64, float64) makeSwap(&floatSwap) fmt.Println(floatSwap(2.72, 3.14)) }
原理
認清楚type與interface
go是一個靜態型別語言,每一個變數有static type
,比如int
,float
,何謂static type
,我的理解是一定長度的二進位制塊與解釋。比如同樣的二進位制塊00000001
在bool型別中意思是true
。而在int型別中解釋是1
.我們看看以下這個最簡單的例子
type MyInt int var i int var j MyInt
i,j在記憶體中都是用int這一個底層型別來表示,但是在實際編碼過程中,在編譯的時候他們並非一個型別,你不能直接將i的值賦給j。是不是有點奇怪,你執行的時候編譯器會告訴你,你不能將MyInt型別的值賦給int型別的值。這個type
不是class
也不是python
的type
.
interface
作為一種特殊的type
, 表示方法的集合。一個interface
的值可以存任何確定的值只要這個值實現了interface
的方法。interface{}
某些時候和Java的Object
好想,實際上interface
是有兩部分內容組成的,實際的值
和值的具體型別
。這也可以解釋為什麼下面這段程式碼和其他語言都不一樣。具體關於interface的原理可以參考ofollow,noindex">go data structures: interfaces
。
package main import ( "fmt" ) type A interface { x(param int) } type B interface { y(param int) } type AB struct { } func (ab *AB) x(param int) { fmt.Printf("%p", ab) fmt.Println(param) } func (ab *AB) y(param int) { fmt.Printf("%p", ab) fmt.Println(param) } func printX(a A){ fmt.Printf("%p", a) a.x(2) } func printY(b B){ fmt.Printf("%p", b) b.y(3) } func main() { var ab = new(AB) printX(ab) printY(ab) var aInfImpl A var bInfImpl B aInfImpl = new(AB) //bInfImpl = aInfImpl會報錯 bInfImpl = aInfImpl.(B) bInfImpl.y(2) }
golang反射三定理
把一個interface
值,拆分出反射物件
反射僅僅用於檢查介面值的(Value, Type)。如上一章提到的兩個方法ValueOf
和TypeOf
。通過ValueOf
我門可以輕易的拿到Type
package main import ( "fmt" "reflect" ) func main() { var x float64 = 3.4 fmt.Println("type:", reflect.TypeOf(x)) }
這段程式碼輸出
type: float64
那麼問題就來了,介面在哪裡?只是申明瞭一個float64
的變數。哪裡來的interface
。有的,答案就藏在TypeOf
引數裡面
func TypeOf(i interface{}) Type
當我們呼叫reflect.TypeOf(x)
, x首先被存在一個空的interface
裡面。然後在被當作引數傳到函式執行棧內。**reflect.TypeOf
解開這個interface
的pair
然後恢復出型別資訊**
把反射物件組合成一個介面值
就像鏡面反射一樣,go的反射是可逆的。給我一個reflect.Value
。我們能夠恢復出一個interface
的值。事實上,以下函式乾的事情就是將Value
和Type
組狠起來塞到interface
裡面去。所以我們可以
y := v.Interface().(float64) // y will have type float64. fmt.Println(y)
接下來就是見證奇蹟的時刻。fmt.Println
和fmt.Printf
的引數都是interface{}
。我們真正都不需要將上面例子的y
轉化成明確的float64
。我就可以去列印他比如
fmt.Println(v.Interface())
甚至我們的interface
藏著的那個type
是float64
。我們可以直接用佔位符來列印
fmt.Println("Value is %7.le\n", v.Interface())
再重複一邊,沒有必要將v.Interface()
的型別強轉到float64
;這個空的interface{}
包含了concrete type
。函式呼叫會恢復出來
要改變一個反射物件,其值必須是可設定的
第三條比較讓你比較困惑。不過如果我們理解了第一條,那麼這條其實非常好理解。先看一下下面這個例子
var x float64 = 3.4 v := reflect.ValueOf(x) v.SetFloat(7.1) // Error: will panic.
如果執行這段程式碼,你會發現出現panic
以下資訊
panic: reflect.Value.SetFloat using unaddressable value
可設定性是一個好東西,但不是所有reflect.Value
都有他...可以通過CanSet
函式來獲取是否可設定
var x float64 = 3.4 v := reflect.ValueOf(x) fmt.Println("settability of v:", v.CanSet())
那麼到底為什麼要有一個可設定性呢?可定址才可設定,我們在用reflect.ValueOf
時候,實際上是函式傳值。獲取x的反射物件,實際上是另外一個float64
的記憶體的反射物件。這個時候我們再去設定該反射物件的值,沒有意義。這段記憶體並不是你申明的那個x。