-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStructReflect.go
More file actions
42 lines (35 loc) · 970 Bytes
/
StructReflect.go
File metadata and controls
42 lines (35 loc) · 970 Bytes
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"
)
// 使用反射输出结构体的名字以及各个字段的名字类型以及Tag信息
type ReflectStruct struct {
Name string
Age int
saving float64
Cars []string `flagTest:"bmw"`
}
func main() {
structType := reflect.ValueOf(ReflectStruct{}).Type()
// 获取结构体的名字
fmt.Println(structType.Name())
// 获取结构体的每个字段
for i := 0; i < structType.NumField(); i++ {
subField := structType.Field(i)
fmt.Println(subField.Type, " ", subField.Name, " ", subField.IsExported())
if subField.Tag != "" {
fmt.Println(subField.Tag.Get("flagTest"))
}
}
testSturct := ReflectStruct{
Name: "test",
Age: 12,
saving: 1000.0,
Cars: []string{"bmw", "other"},
}
// 将反射转变成原有的结构体,反射空间到原有的变量空间的转换
structValue := reflect.ValueOf(testSturct)
test := structValue.Interface().(ReflectStruct)
fmt.Println(test.Age)
}