ctx = context.TODO()cmd := exec.CommandContext(ctx, <some_cmd>, <some_arg>)fmt.Println(ctx.Err())ctx.Err() 是否会在 ctx 为 context.TODO() 时变为非零?
1 回答

浮云间
TA贡献1829条经验 获得超4个赞
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
}
- 1 回答
- 0 关注
- 66 浏览
添加回答
举报
0/150
提交
取消