ctx = context.TODO()
cmd := exec.CommandContext(ctx, <some_cmd>, <some_arg>)
fmt.Println(ctx.Err())
ctx.Err() 是否會在 ctx 為 context.TODO() 時變為非零?
uj5u.com熱心網友回復:
context.TODO().Err() 將始終回傳 nil,正如在源代碼中很容易看到的那樣:
package context
// An emptyCtx is never canceled, has no values, and has no deadline.
type emptyCtx int
func (*emptyCtx) Err() error {
return nil
}
// ...
var (
todo = new(emptyCtx)
)
// ...
// 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).
func TODO() Context {
return todo
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/520331.html
標籤:去去上下文
