uitls.go 448 B

12345678910111213141516
  1. package utils
  2. import "reflect"
  3. // 把value的值按字段分配给 binding
  4. func StructAssign(binding interface{}, value interface{}) {
  5. bVal := reflect.ValueOf(binding).Elem()
  6. vVal := reflect.ValueOf(value).Elem()
  7. vTypeOfT := vVal.Type()
  8. for i := 0; i < vVal.NumField(); i++ {
  9. name := vTypeOfT.Field(i).Name
  10. if ok := bVal.FieldByName(name).IsValid(); ok {
  11. bVal.FieldByName(name).Set(reflect.ValueOf(vVal.Field(i).Interface()))
  12. }
  13. }
  14. }