# 5.8 上下文
從 Go 語言本身的設計來看,盡管我們能夠輕松的創建一個 Goroutine, 但對一個已經啟動的 Goroutine 做取消操作卻并不容易,例如:
```
go func() {
// 如何從其他 Goroutine 通知并結束該 Goroutine 呢?
// ...
}()
```
通過 Channel 與 Select 這一過程間通信原語,我們可以使用空結構信號`struct{}`來通知一個正在執行的 Goroutine:
```
cancel := make(chan struct{})
go func() {
done := make(chan struct{}, 1)
go func() {
defer func() {
done <- struct{}{}
}()
do() // 執行需要執行的操作
}()
select {
case <-cancel:
// 如果提前被取消,則等待執行完畢
// 并撤銷已經執行的操作
<-done
undo()
case <-done:
// 如果順利結束,則結束執行
close(done)
}
}
// ... 出于某些原因希望執行取消操作
cancel <- struct{}{}
```
這樣的要求很常見,例如某個 Web 請求被中斷,服務端正在請求的資源需要做取消操作等等。那我們能否將這一同步模式進一步抽象為接口,作為一種基于通信的同步模式呢?上下文 Context 包就提供了這樣一組在 Goroutine 間進行值傳播的方法。
## 上下文接口
```
type Context interface {
// 截止日期返回應取消代表該上下文完成的工作的時間。如果未設置截止日期,則截止日期返回ok == false。連續調用Deadline會返回相同的結果。
Deadline() (deadline time.Time, ok bool)
// Done 返回一個 channel,當代表該上下文完成的工作應被取消時,該通道將關閉。
// 如果此上下文永遠無法取消,則可能會返回 nil。
// 連續調用 Done 將返回相同的值。在取消函數返回之后,完成 channel 的關閉可能會異步發生。
Done() <-chan struct{}
// 如果 Done 未被關閉,則 Err 返回 nil;
// 如果 Done 已被關閉,則 Err 返回一個非空錯誤。
Err() error
// Value 返回了與當前上下文使用 key 相關聯的值;
// 沒有關聯的 key 時將返回 nil。
Value(key interface{}) interface{}
}
type Context interface { Deadline() (deadline time.Time, ok bool) Done() <-chan struct{} Err() error Value(key interface{}) interface{} } var Canceled = errors.New(“context canceled”) var DeadlineExceeded error = deadlineExceededError{} type CancelFunc func()
func Background() Context func TODO() Context func WithCancel(parent Context) (ctx Context, cancel CancelFunc) func WithTimeout(parent Context, timeout time.Duration) (Context, CancelFunc) func WithDeadline(parent Context, d time.Time) (Context, CancelFunc) func WithValue(parent Context, key, val interface{}) Context
```
## 上下文及其衍生品
ctx := context.Background() ctx.WithTimeout(time.Second) ctx.WithDeadline(time.Now()) ctx.WithValue(k, v) ctx.Cancel()
[https://github.com/golang/go/issues/14660](https://github.com/golang/go/issues/14660)[https://github.com/atdiar/goroutine/tree/master/execution](https://github.com/atdiar/goroutine/tree/master/execution)[https://groups.google.com/forum/#](https://groups.google.com/forum/#)!searchin/golang-dev/context$20package|sort:date/golang-dev/JgnR5hrDCu0/pyqbkYfSCQAJ[https://github.com/golang/go/issues/16209](https://github.com/golang/go/issues/16209)[https://github.com/golang/go/issues/8082](https://github.com/golang/go/issues/8082)[https://dave.cheney.net/2017/08/20/context-isnt-for-cancellation](https://dave.cheney.net/2017/08/20/context-isnt-for-cancellation)[https://github.com/golang/go/issues/21355](https://github.com/golang/go/issues/21355)[https://github.com/golang/go/issues/29011](https://github.com/golang/go/issues/29011)[https://github.com/golang/go/issues/28342](https://github.com/golang/go/issues/28342)[https://blog.labix.org/2011/10/09/death-of-goroutines-under-control](https://blog.labix.org/2011/10/09/death-of-goroutines-under-control)[https://godoc.org/gopkg.in/tomb.v2](https://godoc.org/gopkg.in/tomb.v2)[https://blog.golang.org/context](https://blog.golang.org/context)[https://zhuanlan.zhihu.com/p/26695984](https://zhuanlan.zhihu.com/p/26695984)[https://www.flysnow.org/2017/05/12/go-in-action-go-context.html](https://www.flysnow.org/2017/05/12/go-in-action-go-context.html)[https://juejin.im/post/5a6873fef265da3e317e55b6](https://juejin.im/post/5a6873fef265da3e317e55b6)[https://cloud.tencent.com/developer/section/1140703](https://cloud.tencent.com/developer/section/1140703)[https://siadat.github.io/post/context](https://siadat.github.io/post/context)[https://rakyll.org/leakingctx/](https://rakyll.org/leakingctx/)[https://dreamerjonson.com/2019/05/09/golang-73-context/index.html](https://dreamerjonson.com/2019/05/09/golang-73-context/index.html)[https://brantou.github.io/2017/05/19/go-concurrency-patterns-context/](https://brantou.github.io/2017/05/19/go-concurrency-patterns-context/)[http://p.agnihotry.com/post/understanding\_the\_context\_package\_in\_golang/](http://p.agnihotry.com/post/understanding_the_context_package_in_golang/)[https://faiface.github.io/post/context-should-go-away-go2/](https://faiface.github.io/post/context-should-go-away-go2/)[https://juejin.im/post/5c1514c86fb9a049b82a5acb](https://juejin.im/post/5c1514c86fb9a049b82a5acb)[https://segmentfault.com/a/1190000017394302](https://segmentfault.com/a/1190000017394302)[https://36kr.com/p/5073181](https://36kr.com/p/5073181)[https://zhuanlan.zhihu.com/p/60180409](https://zhuanlan.zhihu.com/p/60180409)
- 第一部分 :基礎篇
- 第1章 Go語言的前世今生
- 1.2 Go語言綜述
- 1.3 順序進程通訊
- 1.4 Plan9匯編語言
- 第2章 程序生命周期
- 2.1 從go命令談起
- 2.2 Go程序編譯流程
- 2.3 Go 程序啟動引導
- 2.4 主Goroutine的生與死
- 第3 章 語言核心
- 3.1 數組.切片與字符串
- 3.2 散列表
- 3.3 函數調用
- 3.4 延遲語句
- 3.5 恐慌與恢復內建函數
- 3.6 通信原語
- 3.7 接口
- 3.8 運行時類型系統
- 3.9 類型別名
- 3.10 進一步閱讀的參考文獻
- 第4章 錯誤
- 4.1 問題的演化
- 4.2 錯誤值檢查
- 4.3 錯誤格式與上下文
- 4.4 錯誤語義
- 4.5 錯誤處理的未來
- 4.6 進一步閱讀的參考文獻
- 第5章 同步模式
- 5.1 共享內存式同步模式
- 5.2 互斥鎖
- 5.3 原子操作
- 5.4 條件變量
- 5.5 同步組
- 5.6 緩存池
- 5.7 并發安全散列表
- 5.8 上下文
- 5.9 內存一致模型
- 5.10 進一步閱讀的文獻參考
- 第二部分 運行時篇
- 第6章 并發調度
- 6.1 隨機調度的基本概念
- 6.2 工作竊取式調度
- 6.3 MPG模型與并發調度單
- 6.4 調度循環
- 6.5 線程管理
- 6.6 信號處理機制
- 6.7 執行棧管理
- 6.8 協作與搶占
- 6.9 系統監控
- 6.10 網絡輪詢器
- 6.11 計時器
- 6.12 非均勻訪存下的調度模型
- 6.13 進一步閱讀的參考文獻
- 第7章 內存分配
- 7.1 設計原則
- 7.2 組件
- 7.3 初始化
- 7.4 大對象分配
- 7.5 小對象分配
- 7.6 微對象分配
- 7.7 頁分配器
- 7.8 內存統計
- 第8章 垃圾回收
- 8.1 垃圾回收的基本想法
- 8.2 寫屏幕技術
- 8.3 調步模型與強弱觸發邊界
- 8.4 掃描標記與標記輔助
- 8.5 免清掃式位圖技術
- 8.6 前進保障與終止檢測
- 8.7 安全點分析
- 8.8 分代假設與代際回收
- 8.9 請求假設與實務制導回收
- 8.10 終結器
- 8.11 過去,現在與未來
- 8.12 垃圾回收統一理論
- 8.13 進一步閱讀的參考文獻
- 第三部分 工具鏈篇
- 第9章 代碼分析
- 9.1 死鎖檢測
- 9.2 競爭檢測
- 9.3 性能追蹤
- 9.4 代碼測試
- 9.5 基準測試
- 9.6 運行時統計量
- 9.7 語言服務協議
- 第10章 依賴管理
- 10.1 依賴管理的難點
- 10.2 語義化版本管理
- 10.3 最小版本選擇算法
- 10.4 Vgo 與dep之爭
- 第12章 泛型
- 12.1 泛型設計的演進
- 12.2 基于合約的泛型
- 12.3 類型檢查技術
- 12.4 泛型的未來
- 12.5 進一步閱讀的的參考文獻
- 第13章 編譯技術
- 13.1 詞法與文法
- 13.2 中間表示
- 13.3 優化器
- 13.4 指針檢查器
- 13.5 逃逸分析
- 13.6 自舉
- 13.7 鏈接器
- 13.8 匯編器
- 13.9 調用規約
- 13.10 cgo與系統調用
- 結束語: Go去向何方?