12345678910111213141516 |
- package utils
- import "reflect"
- // 把value的值按字段分配给 binding
- func StructAssign(binding interface{}, value interface{}) {
- bVal := reflect.ValueOf(binding).Elem()
- vVal := reflect.ValueOf(value).Elem()
- vTypeOfT := vVal.Type()
- for i := 0; i < vVal.NumField(); i++ {
- name := vTypeOfT.Field(i).Name
- if ok := bVal.FieldByName(name).IsValid(); ok {
- bVal.FieldByName(name).Set(reflect.ValueOf(vVal.Field(i).Interface()))
- }
- }
- }
|