reflect.go 489 B

1234567891011121314151617181920
  1. package utils
  2. import (
  3. "reflect"
  4. )
  5. func ObjToMap(bean interface{}, filter func(fieldName string) string) map[string]interface{} {
  6. value := reflect.Indirect(reflect.ValueOf(bean))
  7. if value.Kind() != reflect.Struct {
  8. panic("the bean mush struct")
  9. }
  10. _type := value.Type()
  11. fieldNum := value.NumField()
  12. _map := make(map[string]interface{}, fieldNum)
  13. for x := 0; x < fieldNum; x++ {
  14. field := _type.Field(x)
  15. _map[filter(field.Name)] = value.Field(x).Interface()
  16. }
  17. return _map
  18. }