copy.go 420 B

12345678910111213141516171819
  1. package utils
  2. import "reflect"
  3. func CopyStructMembers(dest, src interface{}) {
  4. destValue := reflect.ValueOf(dest).Elem()
  5. srcValue := reflect.ValueOf(src).Elem()
  6. // typeOfSrc := srcValue.Type()
  7. for i := 0; i < destValue.NumField(); i++ {
  8. destField := destValue.Field(i)
  9. srcField := srcValue.Field(i)
  10. if destField.CanSet() && srcField.Type() == destField.Type() {
  11. destField.Set(srcField)
  12. }
  13. }
  14. return
  15. }