电子灵魂

golang,go,C#,JAVA,PYTHON,PHP

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
        //context.WithDeadline() // 指定一个终止时间 time
	//context.WithTimeout() // 自定义超时时间 time
	//context.WithValue() // 自定义一个键值对 取值 map[key]value
	//ctx,cancel:=context.WithCancel(context.Background()) // 获取一个终止函数 
var wg sync.WaitGroup
func handleSearch(ctx context.Context) {
	loop:
		for{
			select {
			case <-ctx.Done():
				break loop
			default:
			}
			fmt.Println("working……")
			time.Sleep(time.Second)

		}
}

func main() {
	ctx, cancel:=context.WithCancel(context.Background())
	wg.Add(1)
	go handleSearch(ctx)
	time.Sleep(time.Second*5)
	cancel()
	wg.Wait()
	fmt.Println("over")
}

posted on 2021-12-27 13:13  conncent  阅读(119)  评论(0编辑  收藏  举报