2022-6-2 实习Day50

1、清算后端报文查询排序sql调试报错,不可直接修改sql语句,定位封装排序代码进行条件筛选代码修改 –3小时 100%

2、贸金报文查询相关列表排列混乱原因查找,定位至后端条件筛选封装代码,添加查询条件。 –1小时 100%

3、代理行业务进行测试,问题罗列与查找。–2小时 60%


Golang编程学习(part 44)

1、反射的最佳实践1

使用反射来遍历结构体的字段,调用结构体的方法,并获取结构体标签的值

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
package main
import (
"fmt"
"reflect"
)

type Monst struct {
Name string `json:"name"`
Age int `json:"age"`
Score float32 `json:"成绩"`
Sex string
}

func main() {
monst := Monst{
Name: "黄鼠狼精",
Age: 400,
Score: 30.8,
Sex: "男",
}
TestStruct(monst)
}

//方法,返回两个数的和
func (m Monst) GetSum(n1, n2 int) int {
return n1 + n2
}

//方法,接收四个值,给s赋值
func (m Monst) Set(name string, age int, score float32, sex string) {
m.Name = name
m.Age = age
m.Score = score
m.Sex = sex
}

//方法,显示值
func (m Monst) Print() {
fmt.Println("----start----")
fmt.Println(m)
fmt.Println("----end----")
}

func TestStruct(a interface{}) {
//获取 reflect.Type类型
typ := reflect.TypeOf(a)

//获取reflect.Value类型
val := reflect.ValueOf(a)

//获取到a对应的类别
kd := val.Kind()

//如果传入的不是struct,就退出
if kd != reflect.Struct {
fmt.Println("expect struct")
return
}

//获取到该结构体有几个字段
num := val.NumField()
//val.Elem().Field(0).SetString("白象精")

fmt.Printf("结构体有%v个fields\n", num)

//遍历结构体到所有字段
for i := 0; i < num; i++ {
fmt.Println()
fmt.Printf("Field %d: 值为=%v\n", i, val.Field(i))

//获取到struct标签,注意需要通过reflect.Type来获取tag标签的值
tagVal := typ.Field(i).Tag.Get("json")

//如果该字段有tag标签就显示,否则就不显示
if tagVal != "" {
fmt.Printf("Field %d: tag为=%v\n", i, tagVal)
}

//获取到该结构体有多少个方法
numMethod := val.NumMethod()
fmt.Printf("struct has %d methods\n", numMethod)

//方法到排序默认是按照函数名排序(ASCII码)
val.Method(1).Call(nil)

//声明了参数切片 []reflect.Value
var params []reflect.Value
params = append(params, reflect.ValueOf(10))
params = append(params, reflect.ValueOf(40))
values := val.Method(0).Call(params)
fmt.Println("res=", values[0].Int())
}
}

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
结构体有4个fields

Field 0: 值为=黄鼠狼精
Field 0: tag为=name
struct has 3 methods
----start----
{黄鼠狼精 400 30.8 男}
----end----
res= 50

Field 1: 值为=400
Field 1: tag为=age
struct has 3 methods
----start----
{黄鼠狼精 400 30.8 男}
----end----
res= 50

Field 2: 值为=30.8
Field 2: tag为=成绩
struct has 3 methods
----start----
{黄鼠狼精 400 30.8 男}
----end----
res= 50

Field 3: 值为=男
struct has 3 methods
----start----
{黄鼠狼精 400 30.8 男}
----end----
res= 50

2、反射的最佳实践2

使用反射的方式来获取结构体的 tag 标签, 遍历字段的值,修改字段值,调用结构体方法(要求: 通过传递地址的方式完成, 在前面案例上修改即可)

1
2
3
4
5
6
7
8
9
10
11
12
func TestRelect(t *testing.T) {
monster := Monster{
Name: "老牛",
Age: 300,
Sal: 394,
Sex: "男",
}

valueOf := reflect.ValueOf(&monster)
valueOf.Elem().Field(0).SetString("老鬼")
fmt.Println(monster)
}

3、反射的最佳实践3

定义了两个函数test1和test2,定义一个适配器函数用作统一处理接口

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
package pak

import (
"reflect"
"testing"
)

func TestReflectFunc(t *testing.T) {
call1 := func(v1 int, v2 int) {
t.Log(v1, v2)
}

call2 := func(v1 int, v2 int, s string) {
t.Log(v1, v2, s)
}

var (
function reflect.Value
intValue []reflect.Value
n int
)

bridge := func(call interface{}, args ...interface{}) {
n = len(args)
intValue = make([]reflect.Value, n)
for i := 0; i < n; i++ {
intValue[i] = reflect.ValueOf(args[i])
}
function = reflect.ValueOf(call)
function.Call(intValue)
}
bridge(call1, 1, 2)
bridge(call2, 1, 2, "test2")
}

4、反射的最佳实践4

使用反射操作任意结构体类型

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
package dfs

import (
"reflect"
"testing"
)

type User struct {
UserId string
Name string
}

func TestReflectStruct(t *testing.T) {
var (
model *User
sv reflect.Value
)

model = &User{}
sv = reflect.ValueOf(model)
t.Log("reflect.ValueOf", sv.Kind().String())
sv = sv.Elem()
t.Log("reflect.ValueOf.Elem", sv.Kind().String())
sv.FieldByName("UserId").SetString("123456")
sv.FieldByName("Name").SetString("123456")
t.Log("model", model)

}


5、反射的最佳实践5

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
package main

import (
"reflect"
"testing"
)

type User struct {
UserId string
Name string
}

func TestReflect(t *testing.T) {
var (
model *User
st reflect.Type
elem reflect.Value
)

//获取类型 *User
st = reflect.TypeOf(model)
// ptr
t.Log("reflect.TypeOf", st.Kind().String())

//st指向到类型
st = st.Elem()
//struct
t.Log("reflect.typeOf.Elem", st.Kind().String())

//New返回一个Value类型值,该值持有一个指向类型为type到新申请到零值的指针
elem = reflect.New(st)
//ptr
t.Log("reflect.New", elem.Kind().String())
//struct
t.Log("reflect.New.Elem", elem.Elem().Kind().String())

//model就是创建的User结构体变量(实例)
//model是*User,它的指向和elem是一样的
model = elem.Interface().(*User)
//获取elem指向的值
elem = elem.Elem()
elem.FieldByName("UserId").SetString("1233455")
elem.FieldByName("Name").SetString("nicky")
t.Log("model model.Name", model, model.Name)

}

/*

=== RUN TestReflect
DK_test.go:23: reflect.TypeOf ptr
DK_test.go:28: reflect.typeOf.Elem struct
DK_test.go:33: reflect.New ptr
DK_test.go:35: reflect.New.Elem struct
DK_test.go:44: model model.Name &{1233455 nicky} nicky
--- PASS: TestReflect (0.00s)
PASS

*/