go context源码解析

go 的context贯穿整个goroutine的运行控制的中枢,能够实现执行的生命周期的控制。接口

Context是一个接口,他派生了context.emptyCtx(TODO),cancelCtx,timeCtx,valueCtx,在parent关闭时,会逐一关闭子context。生命周期

 

cancelCtx结构体it

type cancelCtx struct {   Context   mu       sync.Mutex            // protects following fields   done     chan struct{}         // created lazily, closed by first cancel call   children map[canceler]struct{} // set to nil by the first cancel call   err      error                 // set to non-nil by the first cancel call}context.withCancel返回两个值,context和func,经过func能够控制关闭context,而context能够获取状态.
相关文章
相关标签/搜索