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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
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]) } |
輸出結果:
這個程序看到幾點:
1 TypeOf和ValueOf是獲取Type和Value的方法
2 ValueOf返回的<float64 Value>是爲了說明這裏的value是float64
3 第三個b的定義實現了php中的string->method的方法,爲什麼返回的是reflect.Value[]數組呢?當然是因爲Go的函數可以返回多個值的原因了。
Value的方法和屬性
好了,我們看到Value的Type定義了這麼多Set方法:
下面看這麼個例子:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
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) } |
返回:
這段代碼能有一些事情值得琢磨:
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?
看文檔中的解釋:
好吧,什麼是addressable,and was not obtained by the use of unexported struct fields?
CanSet當Value是可尋址的時候,返回true,否則返回false
看到第二個c和p的例子,我們可以這麼理解:
當前面的CanSet是一個指針的時候(p)它是不可尋址的,但是當是p.Elem()(實際上就是*p),它就是可以尋址的
這個確實有點繞。