Wait
wg :=syncWaitGroup{}
wag.Add(3)
go func(){
defer wg.Done()
//do...
}()
go func(){
defer wg.Done()
//do...
}()
go func(){
defer wg.Done()
//do...
}()
wg.Wait()
Cancel
import "context"
func Proc(ctx context.Context) {
for {
select {
case <-ctx.Done():
return
default:
//do ...
}
}
}
func Execute() {
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
go Proc(ctx)
go Proc(ctx)
go Proc(ctx)
time.Sleep(time.Second)
cancel()
}
有疑问加站长微信联系(非本文作者)