在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問(wèn)答/GO/ Golang 的 context.Background 和 context.TO

Golang 的 context.Background 和 context.TODO 是一樣的行為么

Golang 中的 context 包提供了 Background 方法和 TODO 方法,用來(lái)返回一個(gè)emptyContext

源代碼如下

var (
    background = new(emptyCtx)
    todo       = new(emptyCtx)
)

// Background returns a non-nil, empty Context. It is never canceled, has no
// values, and has no deadline. It is typically used by the main function,
// initialization, and tests, and as the top-level Context for incoming
// requests.
func Background() Context {
    return background
}

// TODO returns a non-nil, empty Context. Code should use context.TODO when
// it's unclear which Context to use or it is not yet available (because the
// surrounding function has not yet been extended to accept a Context
// parameter). TODO is recognized by static analysis tools that determine
// whether Contexts are propagated correctly in a program.
func TODO() Context {
    return todo
}

如上, todo 和background 都是一個(gè)emtyCtx實(shí)例,為什么要把兩者區(qū)分開(kāi)呢,使用效果上有什么不一樣的么?

回答
編輯回答
安若晴

注釋已經(jīng)說(shuō)得很清楚了

2017年2月14日 15:39