结构体拷贝工具
This commit is contained in:
parent
9d16ea5ab6
commit
685b7267f9
|
@ -8,6 +8,7 @@ import (
|
||||||
"lingye-gin/src/config"
|
"lingye-gin/src/config"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"reflect"
|
||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
@ -111,3 +112,18 @@ func VerifySign(c *gin.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// source 有数据的结构体
|
||||||
|
// target 空的结构体
|
||||||
|
func StructCopy(source interface{}, target interface{}) {
|
||||||
|
sourceVal := reflect.ValueOf(source).Elem() // 获取reflect.Type类型
|
||||||
|
targetVal := reflect.ValueOf(target).Elem() // 获取reflect.Type类型
|
||||||
|
vTypeOfT := sourceVal.Type()
|
||||||
|
for i := 0; i < sourceVal.NumField(); i++ {
|
||||||
|
// 在要空的结构体中查询有数据结构体中相同属性的字段,有则修改其值
|
||||||
|
name := vTypeOfT.Field(i).Name
|
||||||
|
if ok := targetVal.FieldByName(name).IsValid(); ok {
|
||||||
|
targetVal.FieldByName(name).Set(reflect.ValueOf(sourceVal.Field(i).Interface()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue