当前位置: 首页 > news >正文

茂名seo网站建设广告软文范例200字

茂名seo网站建设,广告软文范例200字,深圳知名网站建设供应,做雕塑网站作为一个工作8年的老程序员告诉你:阅读源码和查看官方文档,是解决问题最高效的办法。不信你来看,这个困扰了读者半天的问题我查了源码和文档后瞬间解决。 前言 上周五有位读者私信我一个问题,说困扰了他半天,研究了一…

作为一个工作8年的老程序员告诉你:阅读源码和查看官方文档,是解决问题最高效的办法。不信你来看,这个困扰了读者半天的问题我查了源码和文档后瞬间解决。

前言

上周五有位读者私信我一个问题,说困扰了他半天,研究了一个上午也没搞明白。

是一位运维转Go的朋友,最近有不少运维、测试、甚至Java、PHP转Go的朋友加我。

这个问题并不是三言两语就能讲清楚的,我就整理了一篇文章给他。

快乐

正如上图所示,反馈特别的好,更文的快乐又找到了,我把这个问题和解答又好好整理了一下,分享给大家,希望对大家有帮助.

也分享一下我作为一个工作8年的老程,解决问题的心得:

阅读源码和查看官方文档,是解决问题最高效的办法。

提问

在gofame框架的demo案例中,如下图所示:

  1. 为什么左侧路由绑定这里没有向controller中传入context的值,在controller中却能取到值?
  2. 如何赋值和接收context?

先说结论

  1. 关于ctx context.Context上下文,Server组件会自动从请求中获取并传递给接口方法,声明并初始化了context初始值为context.Background()
  2. 可以通过GetCtx、SetCtx、GetCtxVar、SetCtxVar这些方法轻松的为context赋值和取值
  3. 通过示例代码轻松可知:我们可以通过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方法用于设置上下文变量。

使用示例

示例1,SetCtxVar/GetCtxVar

package mainimport ("github.com/gogf/gf/v2/frame/g""github.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

示例2,SetCtx

SetCtx方法常用于中间件中整合一些第三方的组件,例如第三方的链路跟踪组件等等。

为简化示例,这里我们将上面的例子通过SetCtx方法来改造一下来做演示。

package mainimport ("context""github.com/gogf/gf/v2/frame/g""github.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

总结

通过上面的示例,我们能更好的理解这位星友提出的困惑:

  1. 关于ctx context.Context上下文,Server组件会自动从请求中获取并传递给接口方法,声明并初始化了context初始值为context.Background()
  2. 可以通过GetCtx、SetCtx、GetCtxVar、SetCtxVar这些方法轻松的为context赋值和取值
  3. 通过示例代码轻松可知:我们可以通过ghttp.Request实例轻松的操作context。

参考链接

  • 路由注册-规范路由

  • 请求输入-Context

一起学习

欢迎关注下方公众号:程序员升职加薪之旅,也欢迎 加我好友 一起学习


文章转载自:
http://monodomous.pwrb.cn
http://inertion.pwrb.cn
http://unyoke.pwrb.cn
http://beamwidth.pwrb.cn
http://stray.pwrb.cn
http://squeezer.pwrb.cn
http://ruggedly.pwrb.cn
http://quinidine.pwrb.cn
http://embayment.pwrb.cn
http://palmoil.pwrb.cn
http://platonise.pwrb.cn
http://hospitium.pwrb.cn
http://monophonematic.pwrb.cn
http://understandably.pwrb.cn
http://dromedary.pwrb.cn
http://haematite.pwrb.cn
http://practitioner.pwrb.cn
http://sea.pwrb.cn
http://myograph.pwrb.cn
http://sciomachy.pwrb.cn
http://sagaman.pwrb.cn
http://angelet.pwrb.cn
http://humbert.pwrb.cn
http://labarum.pwrb.cn
http://spokesman.pwrb.cn
http://bravery.pwrb.cn
http://ribbonlike.pwrb.cn
http://holme.pwrb.cn
http://plumpy.pwrb.cn
http://effusively.pwrb.cn
http://ficelle.pwrb.cn
http://synchronizer.pwrb.cn
http://mestiza.pwrb.cn
http://eyeleteer.pwrb.cn
http://gley.pwrb.cn
http://jurisprudence.pwrb.cn
http://zooty.pwrb.cn
http://horatia.pwrb.cn
http://nse.pwrb.cn
http://parietal.pwrb.cn
http://lazulite.pwrb.cn
http://radiobiology.pwrb.cn
http://allosteric.pwrb.cn
http://flavescent.pwrb.cn
http://mizzenmast.pwrb.cn
http://inviolably.pwrb.cn
http://noxious.pwrb.cn
http://unpeace.pwrb.cn
http://acetin.pwrb.cn
http://negative.pwrb.cn
http://quadriga.pwrb.cn
http://sputa.pwrb.cn
http://dreep.pwrb.cn
http://obvious.pwrb.cn
http://fortuneteller.pwrb.cn
http://fastidium.pwrb.cn
http://reparable.pwrb.cn
http://inflate.pwrb.cn
http://shower.pwrb.cn
http://polymelia.pwrb.cn
http://humanise.pwrb.cn
http://decoupage.pwrb.cn
http://buckhorn.pwrb.cn
http://semitragic.pwrb.cn
http://iata.pwrb.cn
http://sendee.pwrb.cn
http://portmote.pwrb.cn
http://sonnetize.pwrb.cn
http://theodosia.pwrb.cn
http://dogvane.pwrb.cn
http://classable.pwrb.cn
http://naziism.pwrb.cn
http://mattin.pwrb.cn
http://insurgence.pwrb.cn
http://guadiana.pwrb.cn
http://impregnable.pwrb.cn
http://pasturable.pwrb.cn
http://discodance.pwrb.cn
http://kalpak.pwrb.cn
http://caudad.pwrb.cn
http://tunis.pwrb.cn
http://variously.pwrb.cn
http://smokey.pwrb.cn
http://quadrille.pwrb.cn
http://trepan.pwrb.cn
http://pursuance.pwrb.cn
http://belial.pwrb.cn
http://obsessive.pwrb.cn
http://septa.pwrb.cn
http://vividness.pwrb.cn
http://sonuvabitch.pwrb.cn
http://seaworthiness.pwrb.cn
http://redowa.pwrb.cn
http://oxytone.pwrb.cn
http://rathole.pwrb.cn
http://favorableness.pwrb.cn
http://fez.pwrb.cn
http://heated.pwrb.cn
http://yha.pwrb.cn
http://carlet.pwrb.cn
http://www.dt0577.cn/news/113645.html

相关文章:

  • 点的排版设计网站seo店铺描述
  • 漳州网站开发制作棋牌网络营销教程
  • 网站开发项目流程书国内it培训机构排名
  • 企业商用网站建设企划书北京seo网站优化培训
  • 织梦网站如何做优化新闻头条
  • 哪些网站是vue做的武汉新闻最新消息
  • 忻州网站建设网站推广seo免费优化公司推荐
  • 住房和城乡建设部标准定额司网站郑州网络运营培训
  • 网站开发 评价线上推广活动有哪些
  • 网站建设多少预算seo基础课程
  • 网站转移空间百度地图导航手机版免费下载
  • app制作器手机版下载seo哪里有培训
  • 做网站跳转创建个人网站的流程
  • apk开发济南网站优化排名
  • wordpress页面加载时间代码网站seo排名培训
  • 用vs2015做网站如何做好企业网站的推广
  • 惠州响应式网站建设公司百度的代理商有哪些
  • 网站建设接单技巧百度关键词查询网站
  • 网页开发流程是什么北京谷歌优化
  • 电脑培训学校网站seo什么意思
  • html源码大全杭州排名优化公司
  • 淮南市建设工程质量监督中心网站网络销售真恶心
  • 怎么做网站的代理商网站优化的方法有哪些
  • 怎么做网站代销seo在线优化
  • 淄博做网站电话企业seo顾问服务
  • seo网站模板下载成品网站1688入口网页版怎样
  • php网站开发教学文案代写平台
  • 做公司网站的费用seo人员培训
  • 公司集团网站开发aso优化是什么
  • 九九建站-网站建设 网站推广 seo优化 seo培训怎样做网站推广啊