You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

14 lines
344 B

  1. package tools
  2. import "reflect"
  3. // 利用反射将结构体转化为map
  4. func StructToMap(obj interface{}) map[string]interface{}{
  5. obj1 := reflect.TypeOf(obj)
  6. obj2 := reflect.ValueOf(obj)
  7. var data = make(map[string]interface{})
  8. for i := 0; i < obj1.NumField(); i++ {
  9. data[obj1.Field(i).Name] = obj2.Field(i).Interface()
  10. }
  11. return data
  12. }