網(wǎng)站首頁 編程語言 正文
協(xié)同開發(fā)時本地測試
昨天的文章中提到了Go如何優(yōu)雅的進(jìn)行本地測試,今天分享一下:在多人協(xié)同開發(fā)中,如果大家都進(jìn)行本地測試可能會出現(xiàn)的問題。
最大的問題就是git合并的問題,大家都改這個test文件,就會導(dǎo)致有沖突。
我們可以通過把test文件加到.gitignore中來解決這個問題。
比如,我的測試文件所在目錄是:app/system/script/test.go。 我就在.gitignore中添加:
app/system/script/test.go
這樣我們就不用浪費(fèi)時間在解決git沖突上了。
GoFrame如何優(yōu)雅的獲得方法名
今天又發(fā)現(xiàn)一個優(yōu)雅的記錄錯誤日志的神器:runtime.Caller(0)
我們可以通過這個命令動態(tài)獲取對應(yīng)的方法,從而靈活的記錄錯誤日志,方便跟蹤定位問題。
示例如下:
shared.ApiLog()中第三個參數(shù)就是動態(tài)獲取的方法名。
//上下架 func (s *goodsService) Shelves(req *goods_unify.DoShelvesReq, r *ghttp.Request) (err error) { defer func() { if err != nil { funcName, _, _, _ := runtime.Caller(0) shared.ApiLog(r, "error/client_server_goods", runtime.FuncForPC(funcName).Name(), err.Error()) } }() err = service.GoodsUnify.DoShelves(r.Context(), req) if err != nil { return } return }
巧用中間件
比如在登錄之后將登錄信息寫到上下文中,避免每次請求都攜帶登錄信息。
中間件在登錄之后設(shè)置關(guān)鍵信息到context上下文中
package middileware const ( CtxAppKey = "AK" CtxAppID = "app_id" CtxChannel = "channel_id" ) var Middleware = middlewareShared{} type middlewareShared struct { } func (s *middlewareShared) Sign(r *ghttp.Request) { code = checkSignV2(r) r.Middleware.Next() } func checkSignV2(r *ghttp.Request) (code tools.Code) { code, appKey, applicationInfo, sign, parmas := getSignv2Params(r) if 1 != code.Code { return } bodyBytes, err := ioutil.ReadAll(r.Request.Body) if nil != err { code = code.UnKnow() return } r.Request.Body = ioutil.NopCloser(bytes.NewBuffer(bodyBytes)) // 關(guān)鍵點(diǎn) signRight, signParam := createSignV2(applicationInfo.Data.SecretKey, parmas, string(bodyBytes)) if signRight != sign { code = code.SignErr("算法錯誤") return } r.SetParam("appKey", appKey) r.SetParam("appId", applicationInfo.Data.Id) r.SetCtxVar(CtxAppID, applicationInfo.Data.Id) r.SetCtxVar(CtxChannel, applicationInfo.Data.ChannelId) return }
業(yè)務(wù)邏輯直接通過context直接取值
通過r.Context().Value()
獲取數(shù)據(jù):
//校驗(yàn)請求方權(quán)限 func checkLevel(r *ghttp.Request) (err error) { if gconv.Int(r.Context().Value(middileware.CtxChannel)) !=10 { err = errors.New("沒有權(quán)限") return } return }
case when
當(dāng)需要批量更新數(shù)據(jù)庫時,case when是個不錯的選擇,我再深入了解一下用法,后面單獨(dú)出一篇介紹 case when的文章。
總結(jié)
這篇文章總結(jié)了在協(xié)同開發(fā)中,可以把我們的調(diào)試文件添加到gitignore中,避免和其他同時因?yàn)檎{(diào)試文件而沖突,節(jié)省解決沖突的時間。
通過GoFrame的runtime.Caller(0)獲取方法名。
巧用中間件,避免請求中攜帶登錄信息。
原文鏈接:https://juejin.cn/post/7111695942681657381
相關(guān)推薦
- 2022-10-10 python讀取和保存為excel、csv、txt文件及對DataFrame文件的基本操作指南_py
- 2023-02-03 TypeScript?中?as?const使用介紹_其它
- 2022-11-15 TypeScript數(shù)組實(shí)現(xiàn)棧與對象實(shí)現(xiàn)棧的區(qū)別詳解_其它
- 2022-06-01 Kubernetes(K8S)入門基礎(chǔ)內(nèi)容介紹_云和虛擬化
- 2023-01-03 Kotlin文件讀寫與SharedPreferences存儲功能實(shí)現(xiàn)方法_Android
- 2022-11-17 python實(shí)現(xiàn)excel轉(zhuǎn)置問題詳解_python
- 2022-08-25 C++詳細(xì)講解互斥量與lock_guard類模板及死鎖_C 語言
- 2022-08-23 Python快速從視頻中提取視頻幀的方法詳解_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支