From 685b7267f9a82049ee5d9307d427068372c6f06e Mon Sep 17 00:00:00 2001 From: TianJun Date: Wed, 28 Apr 2021 17:19:00 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=93=E6=9E=84=E4=BD=93=E6=8B=B7=E8=B4=9D?= =?UTF-8?q?=E5=B7=A5=E5=85=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/util/util.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/util/util.go b/src/util/util.go index cf17b99..e70f2d2 100644 --- a/src/util/util.go +++ b/src/util/util.go @@ -8,6 +8,7 @@ import ( "lingye-gin/src/config" "net/http" "net/url" + "reflect" "sort" "strconv" "time" @@ -111,3 +112,18 @@ func VerifySign(c *gin.Context) { 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())) + } + } +}