Guest User

Untitled

a guest
Apr 25th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.61 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "context"
  5. "fmt"
  6. "time"
  7. )
  8.  
  9. func main() {
  10. ctx, cancel := context.WithCancel(context.Background())
  11.  
  12. go func() {
  13. select {
  14. case <-ctx.Done():
  15. fmt.Printf("%v Cancelled \n", time.Now().String())
  16. case <-time.After(1 * time.Microsecond):
  17. fmt.Printf("%v Run \n", time.Now().String())
  18. }
  19. }()
  20.  
  21. fmt.Printf("%v Start\n", time.Now().String())
  22. time.Sleep(2 * time.Microsecond)
  23.  
  24. fmt.Printf("%v Finish\n", time.Now().String())
  25. cancel()
  26. time.Sleep(1 * time.Microsecond)
  27.  
  28. fmt.Printf("%v Exit\n", time.Now().String())
  29. }
Add Comment
Please, Sign In to add comment