为了账号安全,请及时绑定邮箱和手机立即绑定

Golang,context.TODO 会不会出错

Golang,context.TODO 会不会出错

Go
慕森卡 2023-03-07 13:56:47
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

}


查看完整回答
反对 回复 2023-03-07
  • 1 回答
  • 0 关注
  • 66 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号