Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.57 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "fmt"
  5. "time"
  6. )
  7.  
  8. func main() {
  9. test(1)
  10. test(2)
  11. }
  12.  
  13. func test(n int) {
  14. fmt.Println("TEST", n)
  15.  
  16. done := make(chan bool)
  17. go func() {
  18. <-time.After(100 * time.Millisecond)
  19. select {
  20. case <-done:
  21. fmt.Println("channel closed")
  22. default:
  23. done <- true
  24. fmt.Println("channel not closed, writing to channel")
  25. }
  26. fmt.Println("goroutine finished")
  27.  
  28. }()
  29. switch n {
  30. case 1:
  31. fmt.Println("closing channel")
  32. close(done)
  33. case 2:
  34. fmt.Println("reading from channel")
  35. <-done
  36. }
  37.  
  38. <-time.After(200 * time.Millisecond)
  39. fmt.Println("test finished")
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement