-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5_reflect.go
292 lines (268 loc) · 5.77 KB
/
5_reflect.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
* @Author: your name
* @Date: 2021-01-05 16:59:23
* @LastEditTime: 2021-01-17 19:05:19
* @LastEditors: Please set LastEditors
* @Description: In User Settings Edit
* @FilePath: /demo/5_reflect.go
*/
package main
import (
"bufio"
"fmt"
"io"
"os"
"reflect"
"strings"
)
type man struct {
Name string `json:"name"`
Age int `json:"age"`
Hobby string `json:"hobby"`
Salary float32 `json:"salary"`
}
func (m man) Speak() {
fmt.Printf("name: %s\n", m.Name)
}
type speaker interface {
Speak()
}
type aliInt = int32
type mInt int32
// type any interface{}
// GetType 获取类型
func GetType(arg interface{}) (tp, kd string) {
t := reflect.TypeOf(arg)
fmt.Printf("type: %s, kind:%s\n", t.Name(), t.Kind())
return fmt.Sprintf("%s", t.Name()), fmt.Sprintf("%s", t.Kind())
}
// GetValue 获取原始值
func GetValue(arg any) (val any) {
v := reflect.ValueOf(arg)
k := v.Kind()
switch k {
case reflect.Float32:
val = float32(v.Float())
case reflect.Float64:
val = float64(v.Float())
case reflect.String:
val = string(v.String())
case reflect.Int:
val = int(v.Int())
case reflect.Int32:
val = int32(v.Int())
case reflect.Bool:
val = bool(v.Bool())
}
fmt.Printf("val: %v, kind: %v\n", val, k)
return
}
// 修改值
func updateVal(arg any) {
// old := *arg.(prt)
v := reflect.ValueOf(arg)
k := v.Elem().Kind()
fmt.Println(k)
switch k {
case reflect.Float32:
case reflect.Float64:
v.Elem().SetFloat(100.21)
tp := v.Elem().Type().Name()
fmt.Printf("v的类型: %s\n", tp)
case reflect.String:
v.Elem().SetString("liliya")
case reflect.Int:
case reflect.Int32:
v.Elem().SetInt(121)
case reflect.Bool:
v.Elem().SetBool(false)
}
}
// 判断一个接口值是否为空
func isnil(arg any) bool {
return reflect.ValueOf(arg).IsNil()
}
// 判断接口是否有字段或者方法
func hasProtype(arg any, field string) bool {
v := reflect.ValueOf(arg)
_, kind := GetType(arg)
switch kind {
case "struct":
if ok := v.FieldByName(field).IsValid(); ok {
return true
}
if ok := v.MethodByName(field).IsValid(); ok {
return true
}
case "map":
if ok := v.MapIndex(reflect.ValueOf(field)).IsValid(); ok {
return true
}
}
return false
}
// 获取struct结构体的key集合
func getAllKyes(sct any, sl *[]any) {
t := reflect.TypeOf(sct)
num := t.NumField()
if num > 0 {
for i := 0; i < num; i++ {
std := t.Field(i)
*sl = append(*sl, std.Name)
// *sl = append(*sl, [...]any{std.Name, std.Type, std.Index, std.Tag.Get("json")})
}
}
}
// 获取struct结构体的值集合
func getAllValues(sct any, sl *[]any) {
v := reflect.ValueOf(sct)
num := v.NumField()
if num > 0 {
for i := 0; i < num; i++ {
std := v.FieldByIndex([]int{i})
var val any
switch std.Kind() {
case reflect.Float32:
val = float32(std.Float())
case reflect.Float64:
val = float64(std.Float())
case reflect.String:
val = string(std.String())
case reflect.Int:
val = int(std.Int())
case reflect.Int32:
val = int32(std.Int())
case reflect.Bool:
val = bool(std.Bool())
}
*sl = append(*sl, val)
}
}
}
// 获取所有方法,并调用
func getAllFunc(arg any) {
v := reflect.ValueOf(arg)
num := v.NumMethod()
if num > 0 {
for i := 0; i < num; i++ {
v.Method(i).Call([]reflect.Value{})
}
}
}
// conf 发的
type conf struct {
Env string `json:"env" ini:"ENV"`
Path string `json:"path" ini:"PATH"`
Num string `json:"num" ini:"NUM"`
}
// ReadConfig 实现读取配置文件
func ReadConfig(path string, cner interface{}) error {
file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()
buf := bufio.NewReader(file)
var reft = func(slice []string) {
t := reflect.TypeOf(cner)
v := reflect.ValueOf(cner)
if t.Elem().Kind() == reflect.Struct {
number := t.Elem().NumField()
if number > 0 {
for i := 0; i < number; i++ {
field := t.Elem().Field(i)
tag := field.Tag.Get("ini")
value := v.Elem().Field(i)
kind := value.Kind()
if tag == slice[0] {
switch kind {
case reflect.String:
value.SetString(slice[1])
}
}
}
}
}
}
for {
line, err := buf.ReadString('\n')
if err == io.EOF {
if len(line) != 0 {
slice := strings.Split(line, " = ")
reft(slice)
}
break
}
slice := strings.Split(line, " = ")
reft(slice)
}
return nil
}
func getNames(n interface{}) {
v := reflect.ValueOf(n)
fmt.Printf("name-elem: %s\n", v.Elem().Type().Name())
}
func initloi() {
num1 := aliInt(1)
num2 := mInt(2)
bl := true
s := "abc"
fl := 1.01
GetType(s)
GetValue(s)
updateVal(&s)
GetType(num1)
GetValue(num1)
updateVal(&num1)
GetType(num2)
GetValue(num2)
GetValue(fl)
updateVal(&fl)
GetType(bl)
GetValue(bl)
updateVal(&bl)
var st student
st = student{
Name: "小刚",
}
GetType(st)
fmt.Printf("hasProtype-sct: %v\n", hasProtype(st, "Name"))
var ptrin *int
var in int
fmt.Printf("ptrin-new之前是否是nil: %v\n", isnil(ptrin))
fmt.Printf("in是否是nil: %v\n", isnil(&in))
ptrin = new(int)
fmt.Printf("ptrin-new之后是否是nil: %v\n", isnil(ptrin))
var mp map[string]any
fmt.Printf("slice是否是nil: %v\n", isnil(mp))
mp = map[string]any{"name": "jack"}
fmt.Printf("slice是否是nil: %v\n", isnil(mp))
fmt.Printf("hasProtype-map: %v\n", hasProtype(mp, "aame"))
per := man{
Name: "重耳",
Age: 22,
Hobby: "篮球",
Salary: 24157.36,
}
slicKey := []any{}
getAllKyes(per, &slicKey)
slicVal := []any{}
getAllValues(per, &slicVal)
fmt.Printf("slicKey: %v\n", slicKey)
fmt.Printf("slicVal: %v\n", slicVal)
getAllFunc(per)
// sp := speaker(per)
var sp speaker = per
GetType(sp)
type sarr [3]int
arr := sarr{1, 2, 3}
GetType(arr)
var (
str = "abc"
)
rt := []byte(str)
GetType(rt)
var config = conf{}
ReadConfig("config.ini", &config)
fmt.Printf("config: %v\n", config)
}