interface.go 467 B

12345678910111213141516171819202122
  1. package utils
  2. import "reflect"
  3. // 根据参数名获取对应参数的值
  4. func GetValue(data interface{}, name string) interface{} {
  5. value := reflect.ValueOf(data)
  6. for i := 0; i < value.NumField(); i++ {
  7. em := value.Field(i)
  8. if em.Kind() != reflect.Struct {
  9. continue
  10. }
  11. for j := 0; j < em.NumField(); j++ {
  12. varName := em.Type().Field(j).Tag.Get("json")
  13. if varName != name {
  14. continue
  15. }
  16. return em.Field(j).Interface()
  17. }
  18. }
  19. return nil
  20. }