github.com/goccy/go-reflect@v1.2.1-0.20220925055700-4646ad15ec8a/example_test.go (about) 1 // Copyright 2012 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package reflect_test 6 7 import ( 8 "bytes" 9 "encoding/json" 10 "fmt" 11 "io" 12 "os" 13 14 "github.com/goccy/go-reflect" 15 ) 16 17 func ExampleKind() { 18 for _, v := range []interface{}{"hi", 42, func() {}} { 19 switch v := reflect.ValueOf(v); v.Kind() { 20 case reflect.String: 21 fmt.Println(v.String()) 22 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: 23 fmt.Println(v.Int()) 24 default: 25 fmt.Printf("unhandled kind %s", v.Kind()) 26 } 27 } 28 29 // Output: 30 // hi 31 // 42 32 // unhandled kind func 33 } 34 35 func ExampleMakeFunc() { 36 // swap is the implementation passed to MakeFunc. 37 // It must work in terms of reflect.Values so that it is possible 38 // to write code without knowing beforehand what the types 39 // will be. 40 swap := func(in []reflect.Value) []reflect.Value { 41 return []reflect.Value{in[1], in[0]} 42 } 43 44 // makeSwap expects fptr to be a pointer to a nil function. 45 // It sets that pointer to a new function created with MakeFunc. 46 // When the function is invoked, reflect turns the arguments 47 // into Values, calls swap, and then turns swap's result slice 48 // into the values returned by the new function. 49 makeSwap := func(fptr interface{}) { 50 // fptr is a pointer to a function. 51 // Obtain the function value itself (likely nil) as a reflect.Value 52 // so that we can query its type and then set the value. 53 fn := reflect.ValueOf(fptr).Elem() 54 55 // Make a function of the right type. 56 v := reflect.MakeFunc(fn.Type(), swap) 57 58 // Assign it to the value fn represents. 59 fn.Set(v) 60 } 61 62 // Make and call a swap function for ints. 63 var intSwap func(int, int) (int, int) 64 makeSwap(&intSwap) 65 fmt.Println(intSwap(0, 1)) 66 67 // Make and call a swap function for float64s. 68 var floatSwap func(float64, float64) (float64, float64) 69 makeSwap(&floatSwap) 70 fmt.Println(floatSwap(2.72, 3.14)) 71 72 // Output: 73 // 1 0 74 // 3.14 2.72 75 } 76 77 func ExampleStructTag() { 78 type S struct { 79 F string `species:"gopher" color:"blue"` 80 } 81 82 s := S{} 83 st := reflect.TypeOf(s) 84 field := st.Field(0) 85 fmt.Println(field.Tag.Get("color"), field.Tag.Get("species")) 86 87 // Output: 88 // blue gopher 89 } 90 91 func ExampleStructTag_Lookup() { 92 type S struct { 93 F0 string `alias:"field_0"` 94 F1 string `alias:""` 95 F2 string 96 } 97 s := S{} 98 st := reflect.TypeOf(s) 99 for i := 0; i < st.NumField(); i++ { 100 field := st.Field(i) 101 if alias, ok := field.Tag.Lookup("alias"); ok { 102 if alias == "" { 103 fmt.Println("(blank)") 104 } else { 105 fmt.Println(alias) 106 } 107 } else { 108 fmt.Println("(not specified)") 109 } 110 } 111 // Output: 112 // field_0 113 // (blank) 114 // (not specified) 115 } 116 117 func ExampleTypeOf() { 118 // As interface types are only used for static typing, a 119 // common idiom to find the reflection Type for an interface 120 // type Foo is to use a *Foo value. 121 writerType := reflect.TypeOf((*io.Writer)(nil)).Elem() 122 123 fileType := reflect.TypeOf((*os.File)(nil)) 124 fmt.Println(fileType.Implements(writerType)) 125 126 // Output: 127 // true 128 } 129 130 func ExampleStructOf() { 131 typ := reflect.StructOf([]reflect.StructField{ 132 { 133 Name: "Height", 134 Type: reflect.TypeOf(float64(0)), 135 Tag: `json:"height"`, 136 }, 137 { 138 Name: "Age", 139 Type: reflect.TypeOf(int(0)), 140 Tag: `json:"age"`, 141 }, 142 }) 143 144 v := reflect.New(typ).Elem() 145 v.Field(0).SetFloat(0.4) 146 v.Field(1).SetInt(2) 147 s := v.Addr().Interface() 148 149 w := new(bytes.Buffer) 150 if err := json.NewEncoder(w).Encode(s); err != nil { 151 panic(err) 152 } 153 154 fmt.Printf("value: %+v\n", s) 155 fmt.Printf("json: %s", w.Bytes()) 156 157 r := bytes.NewReader([]byte(`{"height":1.5,"age":10}`)) 158 if err := json.NewDecoder(r).Decode(s); err != nil { 159 panic(err) 160 } 161 fmt.Printf("value: %+v\n", s) 162 163 // Output: 164 // value: &{Height:0.4 Age:2} 165 // json: {"height":0.4,"age":2} 166 // value: &{Height:1.5 Age:10} 167 }