package main import ( "context" "sync" "time" ) func main() { ctx, cacnel := context.WithTimeout(context.Background(), time.Second*5) defer cacnel() wg := sync.WaitGroup{} go func() { defer func() { wg.Done() println("goroutine exit") }() select { case <-ctx.Done(): println("context done") return } }() wg.Add(1) wg.Wait() }