Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package main
import (
"context"
"time"
"sync"
"fmt"
)
func main() {
var wg sync.WaitGroup
ctx, cancel := context.WithCancel(context.Background())
ctx, cancel = context.WithTimeout(ctx, 3 * time.Second)
defer cancel()
wg.Add(1)
go func() {
defer wg.Done()
for {
select {
case <-ctx.Done():
fmt.Printf("canceled %s\n", ctx.Err())
return
case <-time.After(5 * time.Second):
fmt.Printf("5 secs passed: %s\n", ctx.Err())
return
}
}
}()
wg.Wait()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment