http://www.cnblogs.com/yjf512/archive/2012/06/10/2544391.html
Go语言的基本语法的使用已经在前几篇陆陆续续学完了,下面可能想写一些Go的标准库的使用了。
先是reflect库。
reflect库的godoc在http://golang.org/pkg/reflect/
首先,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])
}
|
输出结果:
这个程序看到几点:
好了,我们看到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)
}
|
返回:
这段代码能有一些事情值得琢磨:
a是一个结构,b是一个指针。好吧,在Go中,指针的定义和C中是一样的。
这是一个绕路的写法,其实和a.name是一样的意思,主要是要说明一下Value.FieldByName的用法
b是一个指针,指针的ValueOf返回的是指针的Type,它是没有Field的,所以也就不能使用FieldByName
看文档中的解释:
好吧,什么是addressable,and was not obtained by the use of unexported struct fields?
CanSet当Value是可寻址的时候,返回true,否则返回false
看到第二个c和p的例子,我们可以这么理解:
这个确实有点绕。
总而言之,reflect包是开发过程中几乎必备的包之一。能合理和熟练使用它对开发有很大的帮助。