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 }
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{}) { typ := reflect.TypeOf(a)
val := reflect.ValueOf(a)
kd := val.Kind()
if kd != reflect.Struct { fmt.Println("expect struct") return }
num := val.NumField()
fmt.Printf("结构体有%v个fields\n", num)
for i := 0; i < num; i++ { fmt.Println() fmt.Printf("Field %d: 值为=%v\n", i, val.Field(i))
tagVal := typ.Field(i).Tag.Get("json")
if tagVal != "" { fmt.Printf("Field %d: tag为=%v\n", i, tagVal) }
numMethod := val.NumMethod() fmt.Printf("struct has %d methods\n", numMethod)
val.Method(1).Call(nil)
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 )
st = reflect.TypeOf(model) t.Log("reflect.TypeOf", st.Kind().String())
st = st.Elem() t.Log("reflect.typeOf.Elem", st.Kind().String())
elem = reflect.New(st) t.Log("reflect.New", elem.Kind().String()) t.Log("reflect.New.Elem", elem.Elem().Kind().String())
model = elem.Interface().(*User) elem = elem.Elem() elem.FieldByName("UserId").SetString("1233455") elem.FieldByName("Name").SetString("nicky") t.Log("model model.Name", model, model.Name)
}
|