雅安市政建设公司网站,管理培训公司,电商网站建设课程设计实验报告,湖北省住房与城乡建设部网站作为一个工作8年的老程序员告诉你#xff1a;阅读源码和查看官方文档#xff0c;是解决问题最高效的办法。不信你来看#xff0c;这个困扰了读者半天的问题我查了源码和文档后瞬间解决。 前言
上周五有位读者私信我一个问题#xff0c;说困扰了他半天#xff0c;研究了一… 作为一个工作8年的老程序员告诉你阅读源码和查看官方文档是解决问题最高效的办法。不信你来看这个困扰了读者半天的问题我查了源码和文档后瞬间解决。 前言
上周五有位读者私信我一个问题说困扰了他半天研究了一个上午也没搞明白。
是一位运维转Go的朋友最近有不少运维、测试、甚至Java、PHP转Go的朋友加我。
这个问题并不是三言两语就能讲清楚的我就整理了一篇文章给他。
快乐 正如上图所示反馈特别的好更文的快乐又找到了我把这个问题和解答又好好整理了一下分享给大家希望对大家有帮助.
也分享一下我作为一个工作8年的老程解决问题的心得 阅读源码和查看官方文档是解决问题最高效的办法。 提问
在gofame框架的demo案例中如下图所示 为什么左侧路由绑定这里没有向controller中传入context的值在controller中却能取到值如何赋值和接收context
先说结论 关于ctx context.Context上下文Server组件会自动从请求中获取并传递给接口方法声明并初始化了context初始值为context.Background()可以通过GetCtx、SetCtx、GetCtxVar、SetCtxVar这些方法轻松的为context赋值和取值通过示例代码轻松可知我们可以通过ghttp.Request实例轻松的操作context。
解答 问题1. 为什么左侧路由绑定这里没有向controller中传入context的值在controller中却能取到值 先说结论关于ctx context.Context上下文Server组件会自动从请求中获取并传递给接口方法。
关键代码是上图中的s : g.Server():
追踪一下它的源码可以发现声明并初始化了ctx的初始值context.Background() 再带大家看一下context.Background()的源码和注释。
// Background returns a non-nil, empty Context. It is never canceled, has no
// values, and has no deadline. It is typically used by the main function,
// initialization, and tests, and as the top-level Context for incoming
// requests.
func Background() Context {return background
}我们可以发现这里返回了一个non-nil, empty Context 问题2. 如何赋值和接收context 在GoFrame框架中官方推荐的正是使用Context上下文对象来处理流程共享的上下文变量甚至将该对象进一步传递到依赖的各个模块方法中。
该Context对象类型实现了标准库的context.Context接口该接口往往会作为模块间调用方法的第一个参数该接口参数也是Golang官方推荐的在模块间传递上下文变量的推荐方式。
方法列表
func (r *Request) GetCtx() context.Context
func (r *Request) SetCtx(ctx context.Context)
func (r *Request) GetCtxVar(key interface{}, def ...interface{}) *gvar.Var
func (r *Request) SetCtxVar(key interface{}, value interface{})简要说明
GetCtx方法用于获取当前的context.Context对象作用同Context方法。SetCtx方法用于设置自定义的context.Context上下文对象。GetCtxVar方法用于获取上下文变量并可给定当该变量不存在时的默认值。SetCtxVar方法用于设置上下文变量。
使用示例
示例1SetCtxVar/GetCtxVar
package mainimport (github.com/gogf/gf/v2/frame/ggithub.com/gogf/gf/v2/net/ghttp
)const (TraceIdName trace-id
)func main() {s : g.Server()s.Group(/, func(group *ghttp.RouterGroup) {group.Middleware(func(r *ghttp.Request) {//向context中赋值r.SetCtxVar(TraceIdName, 1234567890abcd)r.Middleware.Next()})group.ALL(/, func(r *ghttp.Request) {//从context中取值r.Response.Write(r.GetCtxVar(TraceIdName))})})s.SetPort(8199)s.Run()
}可以看到我们可以通过SetCtxVar和GetCtxVar来设置和获取自定义的变量该变量生命周期仅限于当前请求流程。
执行后访问 http://127.0.0.1:8199/ 页面输出内容为1234567890abcd
示例2SetCtx
SetCtx方法常用于中间件中整合一些第三方的组件例如第三方的链路跟踪组件等等。
为简化示例这里我们将上面的例子通过SetCtx方法来改造一下来做演示。
package mainimport (contextgithub.com/gogf/gf/v2/frame/ggithub.com/gogf/gf/v2/net/ghttp
)const (TraceIdName trace-id
)func main() {s : g.Server()s.Group(/, func(group *ghttp.RouterGroup) {group.Middleware(func(r *ghttp.Request) {ctx : context.WithValue(r.Context(), TraceIdName, 1234567890abcd)r.SetCtx(ctx)r.Middleware.Next()})group.ALL(/, func(r *ghttp.Request) {//看到这里的示例代码更能解答问题1通过ghttp.Request可以轻松获得上下文对象r.Response.Write(r.Context().Value(TraceIdName))// 也可以使用// r.Response.Write(r.GetCtxVar(TraceIdName))})})s.SetPort(8199)s.Run()
}执行后访问 http://127.0.0.1:8199/ 页面输出内容为1234567890abcd
总结
通过上面的示例我们能更好的理解这位星友提出的困惑
关于ctx context.Context上下文Server组件会自动从请求中获取并传递给接口方法声明并初始化了context初始值为context.Background()可以通过GetCtx、SetCtx、GetCtxVar、SetCtxVar这些方法轻松的为context赋值和取值通过示例代码轻松可知我们可以通过ghttp.Request实例轻松的操作context。
参考链接 路由注册-规范路由 请求输入-Context
一起学习 欢迎关注下方公众号程序员升职加薪之旅也欢迎 加我好友 一起学习 文章转载自: http://www.morning.rymd.cn.gov.cn.rymd.cn http://www.morning.zlces.com.gov.cn.zlces.com http://www.morning.dglszn.com.gov.cn.dglszn.com http://www.morning.yhgbd.cn.gov.cn.yhgbd.cn http://www.morning.mqdr.cn.gov.cn.mqdr.cn http://www.morning.qddtd.cn.gov.cn.qddtd.cn http://www.morning.ffwrq.cn.gov.cn.ffwrq.cn http://www.morning.dlurfdo.cn.gov.cn.dlurfdo.cn http://www.morning.jkzjs.cn.gov.cn.jkzjs.cn http://www.morning.wnxqf.cn.gov.cn.wnxqf.cn http://www.morning.mfbcs.cn.gov.cn.mfbcs.cn http://www.morning.gxeqedd.cn.gov.cn.gxeqedd.cn http://www.morning.lqpzb.cn.gov.cn.lqpzb.cn http://www.morning.pkwwq.cn.gov.cn.pkwwq.cn http://www.morning.fllfc.cn.gov.cn.fllfc.cn http://www.morning.gmysq.cn.gov.cn.gmysq.cn http://www.morning.bljcb.cn.gov.cn.bljcb.cn http://www.morning.jrqbr.cn.gov.cn.jrqbr.cn http://www.morning.mfmbn.cn.gov.cn.mfmbn.cn http://www.morning.rdlfk.cn.gov.cn.rdlfk.cn http://www.morning.nuejun.com.gov.cn.nuejun.com http://www.morning.dgxrz.cn.gov.cn.dgxrz.cn http://www.morning.sqfnx.cn.gov.cn.sqfnx.cn http://www.morning.hwlmy.cn.gov.cn.hwlmy.cn http://www.morning.elmtw.cn.gov.cn.elmtw.cn http://www.morning.mdplm.cn.gov.cn.mdplm.cn http://www.morning.hbxnb.cn.gov.cn.hbxnb.cn http://www.morning.xqkjp.cn.gov.cn.xqkjp.cn http://www.morning.tdxlj.cn.gov.cn.tdxlj.cn http://www.morning.mwrxz.cn.gov.cn.mwrxz.cn http://www.morning.fesiy.com.gov.cn.fesiy.com http://www.morning.rjhts.cn.gov.cn.rjhts.cn http://www.morning.clgbb.cn.gov.cn.clgbb.cn http://www.morning.fylsz.cn.gov.cn.fylsz.cn http://www.morning.yhywx.cn.gov.cn.yhywx.cn http://www.morning.mlpmf.cn.gov.cn.mlpmf.cn http://www.morning.inheatherskitchen.com.gov.cn.inheatherskitchen.com http://www.morning.cpfbg.cn.gov.cn.cpfbg.cn http://www.morning.gmgyt.cn.gov.cn.gmgyt.cn http://www.morning.gblrn.cn.gov.cn.gblrn.cn http://www.morning.hpkr.cn.gov.cn.hpkr.cn http://www.morning.kqbjy.cn.gov.cn.kqbjy.cn http://www.morning.rnfwx.cn.gov.cn.rnfwx.cn http://www.morning.pxwjp.cn.gov.cn.pxwjp.cn http://www.morning.cjrmf.cn.gov.cn.cjrmf.cn http://www.morning.xdnhw.cn.gov.cn.xdnhw.cn http://www.morning.nqbcj.cn.gov.cn.nqbcj.cn http://www.morning.pmysp.cn.gov.cn.pmysp.cn http://www.morning.ptzbg.cn.gov.cn.ptzbg.cn http://www.morning.mhpkz.cn.gov.cn.mhpkz.cn http://www.morning.ppdr.cn.gov.cn.ppdr.cn http://www.morning.ygth.cn.gov.cn.ygth.cn http://www.morning.rtbj.cn.gov.cn.rtbj.cn http://www.morning.qmrsf.cn.gov.cn.qmrsf.cn http://www.morning.mlckd.cn.gov.cn.mlckd.cn http://www.morning.yckwt.cn.gov.cn.yckwt.cn http://www.morning.ftsmg.com.gov.cn.ftsmg.com http://www.morning.stpkz.cn.gov.cn.stpkz.cn http://www.morning.ynjhk.cn.gov.cn.ynjhk.cn http://www.morning.chhhq.cn.gov.cn.chhhq.cn http://www.morning.yqkmd.cn.gov.cn.yqkmd.cn http://www.morning.fqpgf.cn.gov.cn.fqpgf.cn http://www.morning.jftl.cn.gov.cn.jftl.cn http://www.morning.hxycm.cn.gov.cn.hxycm.cn http://www.morning.tssmk.cn.gov.cn.tssmk.cn http://www.morning.tbnpn.cn.gov.cn.tbnpn.cn http://www.morning.mwqbp.cn.gov.cn.mwqbp.cn http://www.morning.nclps.cn.gov.cn.nclps.cn http://www.morning.frfnb.cn.gov.cn.frfnb.cn http://www.morning.srmpc.cn.gov.cn.srmpc.cn http://www.morning.hxwhyjh.com.gov.cn.hxwhyjh.com http://www.morning.qrlsy.cn.gov.cn.qrlsy.cn http://www.morning.jxfsm.cn.gov.cn.jxfsm.cn http://www.morning.lpcct.cn.gov.cn.lpcct.cn http://www.morning.dmhs.cn.gov.cn.dmhs.cn http://www.morning.lgnbr.cn.gov.cn.lgnbr.cn http://www.morning.yfnjk.cn.gov.cn.yfnjk.cn http://www.morning.xtyyg.cn.gov.cn.xtyyg.cn http://www.morning.mtsck.cn.gov.cn.mtsck.cn http://www.morning.zycll.cn.gov.cn.zycll.cn