Golang反射獲取結(jié)構(gòu)體的值和修改值的代碼示例
功能:根據(jù)id和反射技術(shù)封裝 創(chuàng)建和更新人的查詢
一、代碼
package coryCommon
import (
"context"
"errors"
"github.com/gogf/gf/v2/container/gvar"
"github.com/tiger1103/gfast/v3/internal/app/system/dao"
"reflect"
)
func New() *coryCom {
return &coryCom{}
}
type coryCom struct{}
// CreateByOrUpdateBy 專門用來(lái)反射結(jié)構(gòu)體中的創(chuàng)建人和更新人,然后返回回去,避免重復(fù)造輪子去寫重復(fù)代碼
/**
*使用實(shí)例代碼
* by := coryCommon.New().CreateByOrUpdateBy(ctx, res)
* infoRes := by.(model.BusCompanyInfoRes)
* res = &infoRes
*
*其中 updateByFieldVal.Set(reflect.ValueOf(updateByValue.Interface().(*gvar.Var).String())) 必須類型一致
*/
func (s *coryCom) CreateByOrUpdateBy(ctx context.Context, data interface{}) (dataRes interface{}) {
// 使用反射獲取 data 的值和類型信息
val := reflect.ValueOf(data)
typ := reflect.TypeOf(data)
// 判斷 data 是否為指針類型,并獲取實(shí)際值
if val.Kind() == reflect.Ptr {
val = val.Elem()
typ = typ.Elem()
}
// 獲取 createBy 字段在結(jié)構(gòu)體中的索引
createByField, ok := typ.FieldByName("CreateBy")
if !ok {
// 如果結(jié)構(gòu)體中不存在 createBy 字段,則直接返回原始值
return data
}
updateByField, ok := typ.FieldByName("UpdateBy")
if !ok {
// 如果結(jié)構(gòu)體中不存在 createBy 字段,則直接返回原始值
return data
}
// 判斷 createBy 字段的類型是否為 string
if createByField.Type.Kind() != reflect.String {
// 如果 createBy 字段的類型不是 string,請(qǐng)根據(jù)需要進(jìn)行相應(yīng)的處理或返回錯(cuò)誤
// 此處假設(shè) createBy 必須為 string 類型,如果不是則返回錯(cuò)誤
return errors.New("createBy字段類型不匹配")
}
// 判斷 updateBy 字段的類型是否為 string
if updateByField.Type.Kind() != reflect.String {
// 如果 createBy 字段的類型不是 string,請(qǐng)根據(jù)需要進(jìn)行相應(yīng)的處理或返回錯(cuò)誤
// 此處假設(shè) createBy 必須為 string 類型,如果不是則返回錯(cuò)誤
return errors.New("updateBy字段類型不匹配")
}
// 獲取原始的 createBy 字段的值并獲取創(chuàng)建人
createId := val.FieldByIndex(createByField.Index).String()
value, err := dao.SysUser.Ctx(ctx).Fields("user_name").Where("id", createId).Value()
if err != nil {
return errors.New("系統(tǒng)異常,請(qǐng)聯(lián)系管理員!")
}
// 設(shè)置 createBy 字段的值為指定參數(shù)
createByValue := reflect.ValueOf(value)
createByFieldVal := val.FieldByIndex(createByField.Index)
createByFieldVal.Set(reflect.ValueOf(createByValue.Interface().(*gvar.Var).String()))
// 獲取原始的 createBy 字段的值并獲取創(chuàng)建人
updateId := val.FieldByIndex(updateByField.Index).String()
value2, err := dao.SysUser.Ctx(ctx).Fields("user_name").Where("id", updateId).Value()
if err != nil {
return errors.New("系統(tǒng)異常,請(qǐng)聯(lián)系管理員!")
}
// 設(shè)置 createBy 字段的值為指定參數(shù)
updateByValue := reflect.ValueOf(value2)
updateByFieldVal := val.FieldByIndex(updateByField.Index)
updateByFieldVal.Set(reflect.ValueOf(updateByValue.Interface().(*gvar.Var).String()))
// 返回修改后的結(jié)構(gòu)體
return val.Interface()
}二、演示


封裝好就可以直接在service層使用了【自己手動(dòng)復(fù)制粘貼】
或者嫌麻煩就放到【代碼生成器】中,這樣就不用每次都去寫這個(gè)查詢了
by := coryCommon.New().CreateByOrUpdateBy(ctx, res) infoRes := by.(model.BusEquipmentEquipmentUnpackingInfoRes) res = &infoRes
到此這篇關(guān)于Golang反射獲取結(jié)構(gòu)體的值和修改值的代碼示例的文章就介紹到這了,更多相關(guān)Golang反射獲取結(jié)構(gòu)體值內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Golang算法問(wèn)題之整數(shù)拆分實(shí)現(xiàn)方法分析
這篇文章主要介紹了Golang算法問(wèn)題之整數(shù)拆分實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了Go語(yǔ)言數(shù)值運(yùn)算與數(shù)組遍歷相關(guān)操作技巧,需要的朋友可以參考下2017-02-02
GO接收GET/POST參數(shù)及發(fā)送GET/POST請(qǐng)求的實(shí)例詳解
這篇文章主要介紹了GO接收GET/POST參數(shù)及發(fā)送GET/POST請(qǐng)求,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12
Go 控制協(xié)程(goroutine)的并發(fā)數(shù)量
控制協(xié)程goroutine的并發(fā)數(shù)量是一個(gè)常見(jiàn)的需求,本文就來(lái)介紹一下Go 控制協(xié)程的并發(fā)數(shù)量,具有一定的參考價(jià)值,感興趣的可以了解一下2025-02-02
Golang并發(fā)發(fā)送HTTP請(qǐng)求的各種方法
在 Golang 領(lǐng)域,并發(fā)發(fā)送 HTTP 請(qǐng)求是優(yōu)化 Web 應(yīng)用程序的一項(xiàng)重要技能,本文探討了實(shí)現(xiàn)此目的的各種方法,從基本的 goroutine 到涉及通道和sync.WaitGroup 的高級(jí)技術(shù),需要的朋友可以參考下2024-02-02
golang通過(guò)反射手動(dòng)實(shí)現(xiàn)json序列化的方法
在 Go 語(yǔ)言中,JSON 序列化和反序列化通常通過(guò)標(biāo)準(zhǔn)庫(kù) encoding/json 來(lái)實(shí)現(xiàn),本文給大家介紹golang 通過(guò)反射手動(dòng)實(shí)現(xiàn)json序列化的方法,感興趣的朋友一起看看吧2024-12-12
goland中使用leetcode插件實(shí)現(xiàn)
本文主要介紹了goland中使用leetcode插件實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04

