Files
WddSuperAgent/agent-common/utils/ReflectUtils.go
2025-03-27 16:09:20 +08:00

22 lines
514 B
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package utils
import "reflect"
// CopySameFields 利用反射将a中的所有同名字段的值 复制到b中的对应字段
func CopySameFields(source, target interface{}) {
va := reflect.ValueOf(source).Elem()
vb := reflect.ValueOf(target).Elem()
for i := 0; i < va.NumField(); i++ {
// 忽略source中 空值的部分
if va.Field(i).IsZero() {
continue
}
fieldName := va.Type().Field(i).Name
if vb.FieldByName(fieldName).IsValid() {
vb.FieldByName(fieldName).Set(va.Field(i))
}
}
}