package main import ( "context" "fmt" "time" "sync" ) func loop(wg *sync.WaitGroup, ctx context.Context) { wg.Add(1) defer wg.Done() //<-ctx.Done() //fmt.Println("done") //return for { select { case <- ctx.Done(): fmt.Println("done") return default: } time.Sleep(1 * time.Second) fmt.Println(time.Now().UTC()) } } func main() { ctx, cancel := context.WithCancel(context.Background()) wg := &sync.WaitGroup{} go loop(wg, ctx) ctx, _ = context.WithCancel(ctx) go loop(wg, ctx) go loop(wg, ctx) time.Sleep(3 * time.Second) cancel() wg.Wait() }