Sunday, February 11, 2018

How do you get struct value on unknown interface{}

How do you get struct value on unknown interface{}


func getProp(d interface{}, label string) (interface{}, bool) {
    switch reflect.TypeOf(d).Kind() {
    case reflect.Struct:
        v := reflect.ValueOf(d).FieldByName(label)
             return v.Interface(), true
    }
   return nil, false
}

Reference:

https://stackoverflow.com/questions/13856539/how-do-you-get-struct-value-on-unknown-interface

No comments: