Golang,context.TODO 会不会出错

ctx = context.TODO()

cmd := exec.CommandContext(ctx, <some_cmd>, <some_arg>)

fmt.Println(ctx.Err())

ctx.Err() 是否会在 ctx 为 context.TODO() 时变为非零?


慕森卡
浏览 67回答 1
1回答

浮云间

context.TODO().Err() 将始终返回 nil,这在源代码中很容易看出:package context// An emptyCtx is never canceled, has no values, and has no deadline.type emptyCtx intfunc (*emptyCtx) Err() error {&nbsp; &nbsp; return nil}// ...var (&nbsp; &nbsp; todo&nbsp; &nbsp; &nbsp; &nbsp;= 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 {&nbsp; &nbsp; return todo}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go