没有合适的资源?快使用搜索试试~ 我知道了~
首页Go Context与任务取消
关联任务的取消 Contex 1、根Context:通过context.Background()创建 2、在Context:context.WithCancel(parentContext)创建 3、ctx,cancel := context.WithCancel(context.Background()) 4、当前Context被取消时,基于它的子context都会被取消 5、接受取消通知 <-ctx.Done() package ch4 import ( context fmt testing time ) func TestCancel(t * testin
资源详情
资源评论
资源推荐

Go Context与任务取消与任务取消
关联任务的取消关联任务的取消
Contex
1、根Context:通过context.Background()创建
2、在Context:context.WithCancel(parentContext)创建
3、ctx,cancel := context.WithCancel(context.Background())
4、当前Context被取消时,基于它的子context都会被取消
5、接受取消通知 <-ctx.Done()
package ch4
import (
"context"
"fmt"
"testing"
"time"
)
func TestCancel(t * testing.T) {
ctx,cancel := context.WithCancel(context.Background())
for i := 0 ; i < 5 ; i++ {
go func(i int , ctx context.Context) {
for {
if isCancelled(ctx){
break
}
time.Sleep(time.Millisecond * 5)
}
fmt.Println(i,"Done")
}(i,ctx)
}
cancel()
time.Sleep(time.Second *1)



















安全验证
文档复制为VIP权益,开通VIP直接复制

评论0