This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| subv = v | |
| destring = f.quoted | |
| for _, i := range f.index { | |
| if subv.Kind() == reflect.Ptr { | |
| if subv.IsNil() { | |
| subv.Set(reflect.New(subv.Type().Elem())) | |
| } | |
| subv = subv.Elem() | |
| } | |
| subv = subv.Field(i) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| switch v.Kind() { | |
| case reflect.Map: | |
| // Map key must either have string kind, have an integer kind, | |
| // or be an encoding.TextUnmarshaler. | |
| t := v.Type() | |
| switch t.Key().Kind() { | |
| case reflect.String, | |
| reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, | |
| reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: | |
| default: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Decoding into nil interface? Switch to non-reflect code. | |
| if v.Kind() == reflect.Interface && v.NumMethod() == 0 { | |
| v.Set(reflect.ValueOf(d.objectInterface())) | |
| return | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func (d *decodeState) unmarshal(v interface{}) (err error) { | |
| <skip over some setup> | |
| rv := reflect.ValueOf(v) | |
| if rv.Kind() != reflect.Ptr || rv.IsNil() { | |
| return &InvalidUnmarshalError{reflect.TypeOf(v)} | |
| } | |
| d.scan.reset() | |
| // We decode rv not rv.Elem because the Unmarshaler interface | |
| // test must be applied at the top level of the value. | |
| d.value(rv) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type Foo struct { | |
| A int | |
| } | |
| func (f Foo) Double() int { | |
| return f.A * 2 | |
| } | |
| type Bar struct { | |
| Foo |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func MakeStruct(vals ...interface{}) interface{} { | |
| var sfs []reflect.StructField | |
| for k, v := range vals { | |
| t := reflect.TypeOf(v) | |
| sf := reflect.StructField{ | |
| Name: fmt.Sprintf("F%d", (k + 1)), | |
| Type: t, | |
| } | |
| sfs = append(sfs, sf) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func MakeTimedFunction(f interface{}) interface{} { | |
| rf := reflect.TypeOf(f) | |
| if rf.Kind() != reflect.Func { | |
| panic("expects a function") | |
| } | |
| vf := reflect.ValueOf(f) | |
| wrapperF := reflect.MakeFunc(rf, func(in []reflect.Value) []reflect.Value { | |
| start := time.Now() | |
| out := vf.Call(in) | |
| end := time.Now() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func main() { | |
| // declaring these vars, so I can make a reflect.Type | |
| intSlice := make([]int, 0) | |
| mapStringInt := make(map[string]int) | |
| // here are the reflect.Types | |
| sliceType := reflect.TypeOf(intSlice) | |
| mapType := reflect.TypeOf(mapStringInt) | |
| // and here are the new values that we are making |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type Foo struct { | |
| A int `tag1:"First Tag" tag2:"Second Tag"` | |
| B string | |
| } | |
| func main() { | |
| greeting := "hello" | |
| f := Foo{A: 10, B: "Salutations"} | |
| gVal := reflect.ValueOf(greeting) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type Foo struct { | |
| A int `tag1:"First Tag" tag2:"Second Tag"` | |
| B string | |
| } | |
| func main() { | |
| sl := []int{1, 2, 3} | |
| greeting := "hello" | |
| greetingPtr := &greeting | |
| f := Foo{A: 10, B: "Salutations"} |