当前位置:网站首页>golang使用反射将一个结构体的数据直接复制到另一个结构体中(通过相同字段)
golang使用反射将一个结构体的数据直接复制到另一个结构体中(通过相同字段)
2022-06-10 14:36:00 【Deng_Xian_Sheng】
// CopyStruct
// dst 目标结构体,src 源结构体
// 必须传入指针,且不能为nil
// 它会把src与dst的相同字段名的值,复制到dst中
func CopyStruct(dst, src interface{
}) {
dstValue := reflect.ValueOf(dst).Elem()
srcValue := reflect.ValueOf(src).Elem()
for i := 0; i < srcValue.NumField(); i++ {
srcField := srcValue.Field(i)
srcName := srcValue.Type().Field(i).Name
dstFieldByName := dstValue.FieldByName(srcName)
if dstFieldByName.IsValid() {
switch dstFieldByName.Kind() {
case reflect.Ptr:
switch srcField.Kind() {
case reflect.Ptr:
if srcField.IsNil() {
dstFieldByName.Set(reflect.Zero(dstFieldByName.Type()))
} else {
dstFieldByName.Set(srcField)
}
default:
dstFieldByName.Set(srcField.Addr())
}
default:
switch srcField.Kind() {
case reflect.Ptr:
if srcField.IsNil() {
dstFieldByName.Set(reflect.Zero(dstFieldByName.Type()))
} else {
dstFieldByName.Set(srcField.Elem())
}
default:
dstFieldByName.Set(srcField)
}
}
}
}
}
边栏推荐
- Flutter drawer learning summary 6
- How the WordPress administrator user name was leaked
- WordPress的管理员用户名是如何泄露的
- orgin框架 笔记
- 2022第十四届南京国际人工智能产品展会
- Generate a dataset of training vectors for doc2vec
- LeetCode_20(括号匹配)
- Euclidean algorithm for finding the greatest common factor
- SIGIR 2022 | HKU and Wuhan University put forward kgcl: a recommendation system based on knowledge map comparative learning
- 【离散数学期复习系列】六、树
猜你喜欢
![[solution] each time the trained model is loaded, the generated vector will be different](/img/30/92ddcda69832df2bd5e335fc3b4fdd.png)
[solution] each time the trained model is loaded, the generated vector will be different

焱融看|混合云环境下,如何实现数据湖最优存储解决方案

Consumption mode of Message Oriented Middleware

Flutter Icon Stack LIsttitle... Learning summary 3

Singleton pattern and special class design

2022广东省安全员A证第三批(主要负责人)考试练习题及在线模拟考试

The interview at the big factory was held at 10:00 a.m. and came out at 10:09 a.m. the question was really too

Primary master-slave table integration process development process

2022 practice questions and online simulation test for the third batch of Guangdong Provincial Safety Officer a certificate (principal)

Allan variance and random error identification
随机推荐
Do you understand all these difficult memory problems?
CG碰撞检测 Collision Testing
【离散数学期复习系列】六、树
JMeter 中如何实现接口之间的关联?
产品开发的早期阶段,是选择开发app还是小程序?
2022年制冷与空调设备运行操作理论题库模拟考试平台操作
2022 practice questions and online simulation test for the third batch of Guangdong Provincial Safety Officer a certificate (principal)
这个牛逼的低代码生成器,现在开源了!
Wechat applet returns to the previous page and transfers parameters
ScrollView 初始化的时候不在最顶部?
【原创】POI 5.x XSSF和HSSF使用自定义字体颜色
[discrete mathematics review series] IV. figure
[logodetection data set processing] (2) draw the label box of the training set picture
Insight technology was selected into the "Aijian · privacy computing manufacturer panorama report" and was rated as a representative manufacturer of financial solutions
what‘t the meaning of “de facto“
2022第十五届南京国际工业自动化展览会
Cell asynchronously invokes method change records
Beijing / Shanghai internal promotion | recruitment of full-time interns in the system and network group of Microsoft Research Asia
Flutter listview, column, row learning personal summary 2
Binary tree and Figure 2