今天就跟大家聊聊有關golang 中 context的作用是什么,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
創(chuàng)新互聯(lián)公司長期為數(shù)千家客戶提供的網(wǎng)站建設服務,團隊從業(yè)經(jīng)驗10年,關注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務;打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為泗陽企業(yè)提供專業(yè)的成都網(wǎng)站設計、做網(wǎng)站,泗陽網(wǎng)站改版等技術服務。擁有10余年豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。
上下文 context.Context
是Go
語言中用來設置截止日期、同步信號,傳遞請求相關值的結構體。上下文與 Goroutine
有比較密切的關系,是 Go
語言中獨特的設計,在其他編程語言中我們很少見到類似的概念。
context.Context
是 Go
語言在 1.7
版本中引入標準庫的接口,該接口定義了四個需要實現(xiàn)的方法,其中包括:
context.Context
被取消的時間,也就是完成工作的截止日期;Channel
,這個
Channel
會在當前工作完成或者上下文被取消后關閉,多次調(diào)用
Done
方法會返回同一個
Channel
;context.Context
結束的原因,它只會在
Done
方法對應的
Channe
l 關閉時返回非空的值;如果
context.Context
被取消,會返回
Canceled
錯誤;如果 c
ontext.Context
超時,會返回
DeadlineExceeded
錯誤;context.Context
中獲取鍵對應的值,對于同一個上下文來說,多次調(diào)用
Value
并傳入相同的
Key
會返回相同的結果,該方法可以用來傳遞請求特定的數(shù)據(jù);type Context interface {
Deadline() (deadline time.Time, ok bool)
Done() <-chan struct{}
Err() error
Value(key interface{}) interface{}
}
context
包中提供的 context.Background
、context.TODO
、context.WithDeadline
和 context.WithValue
函數(shù)會返回實現(xiàn)該接口的私有結構體。
func main() {
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
go handle(ctx, 500*time.Millisecond)
select {
case <-ctx.Done():
fmt.Println("main", ctx.Err())
}
}
func handle(ctx context.Context, duration time.Duration) {
select {
case <-ctx.Done():
fmt.Println("handle", ctx.Err())
case <-time.After(duration):
fmt.Println("process request with", duration)
}
}
Go
context
包中最常用的方法還是 context.Background
、context.TODO
,這兩個方法都會返回預先初始化好的私有變量 background
和 todo
,它們會在同一個 Go
程序中被復用
var (
background = new(emptyCtx)
todo = new(emptyCtx)
)
background 通常用在 main 函數(shù)中,作為所有 context 的根節(jié)點。
todo 通常用在并不知道傳遞什么 context 的情形。例如,調(diào)用一個需要傳遞 context 參數(shù)的函數(shù),你手頭并沒有其他 context 可以傳遞,這時就可以傳遞 todo。這常常發(fā)生在重構進行中,給一些函數(shù)添加了一個 Context 參數(shù),但不知道要傳什么,就用 todo “占個位子”,最終要換成其他 context。
context.WithCancel
函數(shù)能夠從context.Context
中衍生出一個新的子上下文并返回用于取消該上下文的函數(shù)。一旦我們執(zhí)行返回的取消函數(shù),當前上下文以及它的子上下文都會被取消,所有的Goroutine
都會同步收到這一取消信號。
func WithCancel(parent Context) (ctx Context, cancel CancelFunc) {
c := newCancelCtx(parent)
propagateCancel(parent, &c)
return &c, func() { c.cancel(true, Canceled) }
}
func propagateCancel(parent Context, child canceler) {
done := parent.Done()
if done == nil {
return // 父上下文不會觸發(fā)取消信號
}
select {
case <-done:
child.cancel(false, parent.Err()) // 父上下文已經(jīng)被取消
return
default:
}
if p, ok := parentCancelCtx(parent); ok {
p.mu.Lock()
if p.err != nil {
child.cancel(false, p.err)
} else {
p.children[child] = struct{}{}
}
p.mu.Unlock()
} else {
go func() {
select {
case <-parent.Done():
child.cancel(false, parent.Err())
case <-child.Done():
}
}()
}
}
context
包中的 context.WithValue
能從父上下文中創(chuàng)建一個子上下文,傳值的子上下文使用 context.valueCtx
類型
func WithValue(parent Context, key, val interface{}) Context {
if key == nil {
panic("nil key")
}
if !reflectlite.TypeOf(key).Comparable() {
panic("key is not comparable")
}
return &valueCtx{parent, key, val}
}
type valueCtx struct {
Context
key, val interface{}
}
func (c *valueCtx) Value(key interface{}) interface{} {
if c.key == key {
return c.val
}
return c.Context.Value(key)
}
看完上述內(nèi)容,你們對golang 中 context的作用是什么有進一步的了解嗎?如果還想了解更多知識或者相關內(nèi)容,請關注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。
當前文章:golang中context的作用是什么
網(wǎng)頁URL:http://www.chinadenli.net/article18/gechgp.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作、用戶體驗、網(wǎng)站設計公司、自適應網(wǎng)站、ChatGPT、網(wǎng)站改版
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)