<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # 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 &lt;- struct{}{} }() do() // 執行需要執行的操作 }() select { case &lt;-cancel: // 如果提前被取消,則等待執行完畢 // 并撤銷已經執行的操作 &lt;-done undo() case &lt;-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() &lt;-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)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看