logo
  • 教程列表
  • SO官方文檔
  • 上下文
    • 上下文樹表示為有向圖
    • 使用上下文取消工作
  1. StackOverflow 文件
  2. Go 教程
  3. 上下文
  4. 使用上下文取消工作

使用上下文取消工作

Created: November-22, 2018

將具有超時(或使用取消功能)的上下文傳遞給長時間執行的函式可用於取消該函式的工作:

ctx, _ := context.WithTimeout(context.Background(), 200*time.Millisecond)
for {
    select {
    case <-ctx.Done():
        return ctx.Err()
    default:
        // Do an iteration of some long running work here!
    }
}
  • 上下文樹表示為有向圖

Copyright © 2018. All right reserved

tastones.com 备案号:鲁ICP备18045372号-1

  • 關於本站
  • 免責聲明