在 GAE Go 中,為了記錄,我們需要創建一個新的上下文,使用appengine.NewContext(r)which returns context.Context。如何使用此上下文在請求范圍內設置/獲取變量?在 Gorilla 中,Context在上下文中有一個干凈的 Set/Get 函數,這是我想在我的代碼中使用的。但是我不想導入 2 個不同的上下文包。GAE 日志記錄強制您使用context.Context.//handlerFuncfunc 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?}
1 回答

縹緲止盈
TA貢獻2041條經驗 獲得超4個贊
import (
"golang.org/x/net/context"
gorillacontext "github.com/gorilla/context"
)
我知道這是不是你想要的答案,但有沒有辦法解決它,因為“背景下的圍棋標準庫(由App Engine的使用)”包不提供你想要比大猩猩“的功能范圍內”包. 如果要使用定義自己的“上下文”包的其他框架,則需要使用多個導入。
在小組討論中對這兩個上下文提出了一個很好的觀點,因為可能 Gorilla 的上下文被錯誤命名,因為它們都用于不同的目的——“App Engine 一個存儲憑據以發出 RPC 請求;Gorilla 一個只是請求的容器全球性?!?/p>
- 1 回答
- 0 關注
- 170 瀏覽
添加回答
舉報
0/150
提交
取消