Go語言_反射篇


這里的GO使用的版本是1.2

Go語言的基本語法的使用已經在前幾篇陸陸續續學完了,下面可能想寫一些Go的標准庫的使用了。

先是reflect庫。

reflect庫的godoc在http://golang.org/pkg/reflect/

Type和Value

首先,reflect包有兩個數據類型我們必須知道,一個是Type,一個是Value。

Type就是定義的類型的一個數據類型,Value是值的類型

具體的Type和Value里面包含的方法就要看文檔了:

http://golang.org/pkg/reflect/

 

這里我寫了個程序來理解Type和Value:

package main

import(
    "fmt"
    "reflect"
)

type MyStruct struct{
    name string
}

func (this *MyStruct)GetName() string {
    return this.name
}

func main() {
    s := "this is string"
    fmt.Println(reflect.TypeOf(s))
    fmt.Println("-------------------")
    
    fmt.Println(reflect.ValueOf(s))
    var x float64 = 3.4
    fmt.Println(reflect.ValueOf(x))
    fmt.Println("-------------------")
    
    a := new(MyStruct)
    a.name = "yejianfeng"
    typ := reflect.TypeOf(a)

    fmt.Println(typ.NumMethod())
    fmt.Println("-------------------")
    
    b := reflect.ValueOf(a).MethodByName("GetName").Call([]reflect.Value{})
    fmt.Println(b[0])

}

輸出結果:

string
-------------------
this is string
<float64 value>
-------------------
1
-------------------
yejianfeng

 

補充,在Go version 1.5中會返回

string
-------------------
this is string
3.4
-------------------
1
-------------------
yejianfeng

  

這個程序看到幾點:

1 TypeOf和ValueOf是獲取Type和Value的方法

2 第三個b的定義實現了php中的string->method的方法,為什么返回的是reflect.Value[]數組呢?當然是因為Go的函數可以返回多個值的原因了。

Value的方法和屬性

好了,我們看到Value的Type定義了這么多Set方法:

clip_image002

下面看這么個例子:

package main

import(
    "fmt"
    "reflect"
)

type MyStruct struct{
    name string
}

func (this *MyStruct)GetName() string {
    return this.name
}

func main() {
    fmt.Println("--------------")
    var a MyStruct
    b := new(MyStruct)
    fmt.Println(reflect.ValueOf(a))
    fmt.Println(reflect.ValueOf(b))
    
    fmt.Println("--------------")
    a.name = "yejianfeng"
    b.name = "yejianfeng"
    val := reflect.ValueOf(a).FieldByName("name")

    //painc: val := reflect.ValueOf(b).FieldByName("name")
    fmt.Println(val)

    fmt.Println("--------------")
    fmt.Println(reflect.ValueOf(a).FieldByName("name").CanSet())
    fmt.Println(reflect.ValueOf(&(a.name)).Elem().CanSet())
    
    fmt.Println("--------------")
    var c string = "yejianfeng"
    p := reflect.ValueOf(&c)
    fmt.Println(p.CanSet())   //false
    fmt.Println(p.Elem().CanSet())  //true
    p.Elem().SetString("newName")
    fmt.Println(c)
}

返回:

clip_image003

 

這段代碼能有一些事情值得琢磨:

1 為什么a和b的ValueOf返回的是不一樣的?

a是一個結構,b是一個指針。好吧,在Go中,指針的定義和C中是一樣的。

2 reflect.ValueOf(a).FieldByName("name")

這是一個繞路的寫法,其實和a.name是一樣的意思,主要是要說明一下Value.FieldByName的用法

3 val := reflect.ValueOf(b).FieldByName("name") 是有error的,為什么?

b是一個指針,指針的ValueOf返回的是指針的Type,它是沒有Field的,所以也就不能使用FieldByName

4 fmt.Println(reflect.ValueOf(a).FieldByName("name").CanSet())為什么是false?

看文檔中的解釋:

clip_image004

好吧,什么是addressable,and was not obtained by the use of unexported struct fields?

CanSet當Value是可尋址的時候,返回true,否則返回false

看到第二個c和p的例子,我們可以這么理解:

當前面的CanSet是一個指針的時候(p)它是不可尋址的,但是當是p.Elem()(實際上就是*p),它就是可以尋址的

這個確實有點繞。

 

總而言之,reflect包是開發過程中幾乎必備的包之一。能合理和熟練使用它對開發有很大的幫助。

 

20160829 補充:

Go 1.5的reflect Type方法可以看下面這個例子:

package main

import (
	"fmt"
	"reflect"
)

type MyStruct struct {
	name string
}

func (this *MyStruct) GetName() string {
	return this.name
}

type IStruct interface {
	GetName() string
}

func main() {
	// TypeOf
	s := "this is string"
	fmt.Println(reflect.TypeOf(s)) // output: "string"

	// object TypeOf
	a := new(MyStruct)
	a.name = "yejianfeng"
	typ := reflect.TypeOf(a)
	fmt.Println(typ)        // output: "*main.MyStruct"
	fmt.Println(typ.Elem()) // output: "main.MyStruct"

	// reflect.Type Base struct
	fmt.Println(typ.NumMethod())                   // 1
	fmt.Println(typ.Method(0))                     // {GetName  func(*main.MyStruct) string <func(*main.MyStruct) string Value> 0}
	fmt.Println(typ.Name())                        // ""
	fmt.Println(typ.PkgPath())                     // ""
	fmt.Println(typ.Size())                        // 8
	fmt.Println(typ.String())                      // *main.MyStruct
	fmt.Println(typ.Elem().String())               // main.MyStruct
	fmt.Println(typ.Elem().FieldByIndex([]int{0})) // {name main string  0 [0] false}
	fmt.Println(typ.Elem().FieldByName("name"))    // {name main string  0 [0] false} true

	fmt.Println(typ.Kind() == reflect.Ptr)                              // true
	fmt.Println(typ.Elem().Kind() == reflect.Struct)                    // true
	fmt.Println(typ.Implements(reflect.TypeOf((*IStruct)(nil)).Elem())) // true

	fmt.Println(reflect.TypeOf(12.12).Bits()) // 64, 因為是float64

	cha := make(chan int)
	fmt.Println(reflect.TypeOf(cha).ChanDir()) // chan

	var fun func(x int, y ...float64) string
	var fun2 func(x int, y float64) string
	fmt.Println(reflect.TypeOf(fun).IsVariadic())  // true
	fmt.Println(reflect.TypeOf(fun2).IsVariadic()) // false
	fmt.Println(reflect.TypeOf(fun).In(0))         // int
	fmt.Println(reflect.TypeOf(fun).In(1))         // []float64
	fmt.Println(reflect.TypeOf(fun).NumIn())       // 2
	fmt.Println(reflect.TypeOf(fun).NumOut())      // 1
	fmt.Println(reflect.TypeOf(fun).Out(0))        // string

	mp := make(map[string]int)
	mp["test1"] = 1
	fmt.Println(reflect.TypeOf(mp).Key()) //string

	arr := [1]string{"test"}
	fmt.Println(reflect.TypeOf(arr).Len()) // 1

	fmt.Println(typ.Elem().NumField()) // 1

	// MethodByName, Call
	b := reflect.ValueOf(a).MethodByName("GetName").Call([]reflect.Value{})
	fmt.Println(b[0]) // output: "yejianfeng"
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM