第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么 *Request.Context() 存在并在某些情況下返回

為什么 *Request.Context() 存在并在某些情況下返回

Go
ibeautiful 2022-10-10 10:32:00
請幫助我理解 *Request.Context 函數(shù):// Context returns the request's context. To change the context, use// WithContext.//// The returned context is always non-nil; it defaults to the// background context.//// For outgoing client requests, the context controls cancellation.//// For incoming server requests, the context is canceled when the// client's connection closes, the request is canceled (with HTTP/2),// or when the ServeHTTP method returns.func (r *Request) Context() context.Context {    if r.ctx != nil {        return r.ctx    }    return context.Background()}為什么我們需要這個函數(shù)而不是在 *Request 上使用公共屬性?它只是為了封裝,以便沒有人可以更改它,使其有效地只讀嗎?什么時候會返回r.ctx != niland context.Background()?不是每個http請求都保證在收到它的那一刻就有一個上下文嗎?context.Background()如果由于超時或取消而永遠不會“完成”,那有什么用?基本上,為什么不這樣做呢?func (r *Request) Context() context.Context {    return r.ctx}
查看完整描述

1 回答

?
縹緲止盈

TA貢獻2041條經(jīng)驗 獲得超4個贊

  1. 是的,它是用于封裝的。使用WithContextNewReqeustWithContext使用您選擇的上下文創(chuàng)建請求。

  2. r := &http.Request{}創(chuàng)建一個沒有上下文的請求。確保非零返回值對調(diào)用者來說是一種方便。當(dāng)沒有指定其他上下文時,背景上下文是一個合適的默認(rèn)值。


查看完整回答
反對 回復(fù) 2022-10-10
  • 1 回答
  • 0 關(guān)注
  • 108 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號