forked from pibigstar/go-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreflect_test.go
99 lines (85 loc) · 1.99 KB
/
reflect_test.go
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package reflect
import (
"encoding/json"
"fmt"
"reflect"
"testing"
"unsafe"
)
type TestStruct struct {
A int `json:"a"`
B string `json:"b"`
c string `json:"c"`
}
// 通过反射修改非导出字段
func TestChangeNotExportFiled(t *testing.T) {
var r TestStruct
// 获取字段对象
v := reflect.ValueOf(&r).Elem().FieldByName("c")
// 构建指向该字段的可寻址(addressable)反射对象
rv := reflect.NewAt(v.Type(), unsafe.Pointer(v.UnsafeAddr())).Elem()
// 设置值
fv := reflect.ValueOf("pibigstar")
rv.Set(fv)
t.Logf("%+v", r)
}
// 根据反射判断字段类型
func TestInterface(t *testing.T) {
var value interface{}
value = "pibigstar"
switch value.(type) {
case string:
v, ok := value.(string)
if ok {
t.Logf("String ==> %s \n", v)
}
case map[string]string:
v, ok := value.(map[string]string)
if ok {
t.Logf("Map ==> %v \n", v)
}
default:
bs, _ := json.Marshal(value)
t.Logf("Others ==> %s \n", string(bs))
}
}
// 反射基本操作
func TestReflect(t *testing.T) {
var str = "hello world"
v := reflect.ValueOf(str)
// 获取值
t.Log("value:", v)
t.Log("value:", v.String())
// 获取类型
t.Log("type:", v.Type())
t.Log("kind:", v.Kind())
// 修改值
// 判断是否可以修改
canSet := v.CanSet()
t.Log("can set:", canSet)
// 如果想修改其值,必须传递的是指针
v = reflect.ValueOf(&str)
v = v.Elem()
v.SetString("new world") // 不可以直接修改
t.Log("value:", v)
// 通过反射修改结构体
test := TestStruct{A: 23, B: "hello world"}
s := reflect.ValueOf(&test).Elem()
typeOfT := s.Type()
for i := 0; i < s.NumField(); i++ {
f := s.Field(i)
t.Logf("%s: Type ==>%s Value==> %v \n", typeOfT.Field(i).Name, f.Type(), f.Interface())
}
s.Field(0).SetInt(77)
s.Field(1).SetString("new world")
t.Logf("%+v", test)
}
// 获取tag
func TestGetTag(t *testing.T) {
s := TestStruct{}
rt := reflect.TypeOf(s)
for i := 0; i < rt.NumField(); i++ {
f := rt.Field(i)
fmt.Println(f.Tag.Get("json"))
}
}