2
在GAE Go中,为了记录,我们需要使用appengine.NewContext(r)
创建一个新的上下文,它返回context.Context
。Google App Engine context.Context vs gorilla context
如何使用此上下文在请求范围设置/获取变量?在Gorilla中,Context在上下文中有一个干净的Set/Get函数,这是我想在我的代码中使用的。但是我不想导入2个不同的上下文包。
GAE日志迫使您使用context.Context
。
//handlerFunc
func MyFunc(w http.ResponseWriter, r *http.Request) {
ctx := appengine.NewContext(r)
// I want to set request scoped variables in context and pass it to doSomething.
doSomething(ctx,w,r);
}
func doSomething(ctx context.Context, w http.ResponseWriter, r *http.Request) {
log.Debugf(ctx, "Beginning doSomething"); //requires context parameter
// get the request scoped variables from context. How? Use Gorilla Context?
}
创建自定义处理程序类型(将上下文作为参数之一),并让您的路由器或某些中间件(首先在链中)调用NewContext来初始化它。 – elithrar