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能夠獲取狀態.