-
Notifications
You must be signed in to change notification settings - Fork 0
/
reflectUse.go
91 lines (74 loc) · 2.24 KB
/
reflectUse.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
package main
import (
"errors"
"fmt"
"reflect"
"time"
)
type Person struct {
Name string `form:"name" binding:"required"`
Address string `form:"address" binding:"required"`
Birthday time.Time `form:"birthday" json:"birthday" time_format:"2006-01-02" time_utc:"1" binding:"required"`
}
type swaggerInfo struct {
Version string
Host string
BasePath string
Title string
Description string
}
// SwaggerInfo holds exported Swagger Info so clients can modify it
var SwaggerInfo swaggerInfo
func main() {
//testValueOf()
//func1()
//convert()
errorType()
}
func testValueOf() {
SwaggerInfo = swaggerInfo{
Version: "1.11",
Host: "xxxx",
}
v := reflect.ValueOf(SwaggerInfo)
fmt.Printf("v = %+v\n", v)
}
func func1() {
var person Person
typ := reflect.TypeOf(&person)
fmt.Printf("typ = %+v\n", typ)
typelem := reflect.TypeOf(&person).Elem()
fmt.Printf("typelem = %+v\n", typelem)
val := reflect.ValueOf(&person).Elem()
fmt.Printf("val = %+v\n", val)
fmt.Printf("typ.NumField() = %+v\n", typelem.NumField())
fmt.Println(reflect.TypeOf((*Person)(nil)).Elem())
s := "hello"
fmt.Println(reflect.TypeOf(&s).Elem())
// for i := 0; i < typ.NumField(); i++ {
// typeField := typ.Field(i)
// structField := val.Field(i)
// fmt.Printf("typeField = %+v\n", typeField)
// fmt.Printf("structField = %+v\n", structField)
// fmt.Printf("typeField.Tag.Get(time_format) = %+v\n", typeField.Tag.Get("time_format"))
// }
}
func convert() {
var num float64 = 1.2345
pointer := reflect.ValueOf(&num)
value := reflect.ValueOf(num)
// 可以理解为“强制转换”,但是需要注意的时候,转换的时候,如果转换的类型不完全符合,则直接panic
// Golang 对类型要求非常严格,类型一定要完全符合
// 如下两个,一个是*float64,一个是float64,如果弄混,则会panic
convertPointer := pointer.Interface().(*float64)
convertValue := value.Interface().(float64)
fmt.Println(convertPointer)
fmt.Println(convertValue)
}
func errorType() {
errorInterface := reflect.TypeOf((*error)(nil)).Elem()
fmt.Printf("errorInterface = %+v\n", errorInterface)
var err1 interface{}
err1 = errors.New("some err")
fmt.Println(reflect.TypeOf(err1).Implements(errorInterface))
}