12345678910111213141516171819202122 |
- package utils
- import "reflect"
- // 根据参数名获取对应参数的值
- func GetValue(data interface{}, name string) interface{} {
- value := reflect.ValueOf(data)
- for i := 0; i < value.NumField(); i++ {
- em := value.Field(i)
- if em.Kind() != reflect.Struct {
- continue
- }
- for j := 0; j < em.NumField(); j++ {
- varName := em.Type().Field(j).Tag.Get("json")
- if varName != name {
- continue
- }
- return em.Field(j).Interface()
- }
- }
- return nil
- }
|