大家好,這段代碼是模擬客戶端,模擬服務器交互的一部分。我在理解上下文時遇到麻煩。在這里,我使用'WithValue'明確地將跟蹤器接口與上下文“關聯”,然后使用WithContext將其插入到我的請求中。但是,當我檢查請求的上下文是否包含跟蹤器接口時,會返回錯誤“此上下文應包含跟蹤器”。我不了解的上下文和WithValue是什么?var tracker Trackerctx := context.WithValue(context.Background(), contextKey, tracker)req := httptest.NewRequest("GET", "localhost:12345/test", nil)req.Header.Add(HEADER)req = req.WithContext(ctx)_, ok := ctx.Value(contextKey).(Tracker)if !ok { log.Fatal("1: This context should contain a tracker")}
- 2 回答
- 0 關注
- 246 瀏覽
添加回答
舉報
0/150
提交
取消