github.com/456vv/valexa@v1.0.2-0.20200706152242-1fb922d71ce5/Reflect_test.go (about) 1 package valexa 2 3 import ( 4 "testing" 5 "reflect" 6 7 ) 8 9 type TForMethod struct{} 10 func (tf *TForMethod) A1(){} 11 func (tf *TForMethod) A2(){} 12 func (tf *TForMethod) A3(){} 13 func Test_ForMethod(t *testing.T){ 14 var tForMethod = &TForMethod{} 15 t.Logf("\n%s", ForMethod(tForMethod)) 16 } 17 18 type TForType struct{ 19 a int 20 b string 21 c float32 22 } 23 func Test_ForType(t *testing.T){ 24 var tForType = &TForType{} 25 t.Logf("\n%s", ForType(tForType)) 26 } 27 28 29 func Test_TypeSelect(t *testing.T){ 30 var i int = 19 31 t.Logf("%#v", TypeSelect(reflect.ValueOf(i))) 32 } 33 34 func Test_InDirect(t *testing.T){ 35 var i int = 11 36 j := &i 37 b := &j 38 t.Logf("%#v", inDirect(reflect.ValueOf(&b))) 39 } 40 41 42 type A struct { 43 B 44 } 45 type B struct { 46 *C 47 F map[string]string 48 G []string 49 } 50 type C struct { 51 D int 52 } 53 func Test_DepthField(t *testing.T) { 54 a := A{} 55 v, err := DepthField(a, "B", "C", "D") 56 if err == nil { 57 t.Fatalf("错误:由于 *C 默认是空,不可能正确读取到该值(%v)。", v ) 58 } 59 60 v, err = DepthField(a, "B", "C") 61 if err != nil { 62 t.Fatal(err) 63 } 64 65 a.B.F = map[string]string{"1":"a"} 66 v, err = DepthField(a, "B", "F", "1") 67 if err != nil { 68 t.Fatal(err) 69 } 70 71 a.B.G = []string{"1"} 72 v, err = DepthField(a, "B", "G", 0) 73 if err != nil { 74 t.Fatal(err) 75 } 76 }