| | |
| | |
| | |
| |
|
| | package reflect_test |
| |
|
| | import ( |
| | "bytes" |
| | "encoding/json" |
| | "fmt" |
| | "io" |
| | "os" |
| | "reflect" |
| | ) |
| |
|
| | func ExampleKind() { |
| | for _, v := range []any{"hi", 42, func() {}} { |
| | switch v := reflect.ValueOf(v); v.Kind() { |
| | case reflect.String: |
| | fmt.Println(v.String()) |
| | case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: |
| | fmt.Println(v.Int()) |
| | default: |
| | fmt.Printf("unhandled kind %s", v.Kind()) |
| | } |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleMakeFunc() { |
| | |
| | |
| | |
| | |
| | swap := func(in []reflect.Value) []reflect.Value { |
| | return []reflect.Value{in[1], in[0]} |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | makeSwap := func(fptr any) { |
| | |
| | |
| | |
| | fn := reflect.ValueOf(fptr).Elem() |
| |
|
| | |
| | v := reflect.MakeFunc(fn.Type(), swap) |
| |
|
| | |
| | fn.Set(v) |
| | } |
| |
|
| | |
| | var intSwap func(int, int) (int, int) |
| | makeSwap(&intSwap) |
| | fmt.Println(intSwap(0, 1)) |
| |
|
| | |
| | var floatSwap func(float64, float64) (float64, float64) |
| | makeSwap(&floatSwap) |
| | fmt.Println(floatSwap(2.72, 3.14)) |
| |
|
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleStructTag() { |
| | type S struct { |
| | F string `species:"gopher" color:"blue"` |
| | } |
| |
|
| | s := S{} |
| | st := reflect.TypeOf(s) |
| | field := st.Field(0) |
| | fmt.Println(field.Tag.Get("color"), field.Tag.Get("species")) |
| |
|
| | |
| | |
| | } |
| |
|
| | func ExampleStructTag_Lookup() { |
| | type S struct { |
| | F0 string `alias:"field_0"` |
| | F1 string `alias:""` |
| | F2 string |
| | } |
| |
|
| | s := S{} |
| | st := reflect.TypeOf(s) |
| | for field := range st.Fields() { |
| | if alias, ok := field.Tag.Lookup("alias"); ok { |
| | if alias == "" { |
| | fmt.Println("(blank)") |
| | } else { |
| | fmt.Println(alias) |
| | } |
| | } else { |
| | fmt.Println("(not specified)") |
| | } |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleTypeOf() { |
| | |
| | |
| | |
| | writerType := reflect.TypeOf((*io.Writer)(nil)).Elem() |
| |
|
| | fileType := reflect.TypeOf((*os.File)(nil)) |
| | fmt.Println(fileType.Implements(writerType)) |
| |
|
| | |
| | |
| | } |
| |
|
| | func ExampleStructOf() { |
| | typ := reflect.StructOf([]reflect.StructField{ |
| | { |
| | Name: "Height", |
| | Type: reflect.TypeOf(float64(0)), |
| | Tag: `json:"height"`, |
| | }, |
| | { |
| | Name: "Age", |
| | Type: reflect.TypeOf(int(0)), |
| | Tag: `json:"age"`, |
| | }, |
| | }) |
| |
|
| | v := reflect.New(typ).Elem() |
| | v.Field(0).SetFloat(0.4) |
| | v.Field(1).SetInt(2) |
| | s := v.Addr().Interface() |
| |
|
| | w := new(bytes.Buffer) |
| | if err := json.NewEncoder(w).Encode(s); err != nil { |
| | panic(err) |
| | } |
| |
|
| | fmt.Printf("value: %+v\n", s) |
| | fmt.Printf("json: %s", w.Bytes()) |
| |
|
| | r := bytes.NewReader([]byte(`{"height":1.5,"age":10}`)) |
| | if err := json.NewDecoder(r).Decode(s); err != nil { |
| | panic(err) |
| | } |
| | fmt.Printf("value: %+v\n", s) |
| |
|
| | |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleValue_FieldByIndex() { |
| | |
| | |
| | |
| | type user struct { |
| | firstName string |
| | lastName string |
| | } |
| |
|
| | type data struct { |
| | user |
| | firstName string |
| | lastName string |
| | } |
| |
|
| | u := data{ |
| | user: user{"Embedded John", "Embedded Doe"}, |
| | firstName: "John", |
| | lastName: "Doe", |
| | } |
| |
|
| | s := reflect.ValueOf(u).FieldByIndex([]int{0, 1}) |
| | fmt.Println("embedded last name:", s) |
| |
|
| | |
| | |
| | } |
| |
|
| | func ExampleValue_FieldByName() { |
| | type user struct { |
| | firstName string |
| | lastName string |
| | } |
| | u := user{firstName: "John", lastName: "Doe"} |
| | s := reflect.ValueOf(u) |
| |
|
| | fmt.Println("Name:", s.FieldByName("firstName")) |
| | |
| | |
| | } |
| |
|