Guest User

Untitled

a guest
Oct 22nd, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "fmt"
  5. "time"
  6. )
  7.  
  8. func main() {
  9. var (
  10. foo_completed bool = false
  11. bar_completed bool = false
  12. completed bool = false
  13. )
  14. for completed == false {
  15. sharedChan := make(chan string, 1)
  16. go foo(sharedChan)
  17. go bar(sharedChan)
  18. select {
  19. case sharedChan <- "starting foo":
  20. fmt.Println("starting up foo goroutine")
  21. case sharedChan <- "starting bar":
  22. fmt.Println("starting up bar goroutine")
  23. case sharedChan <- "done foo":
  24. fmt.Println("foo goroutine done")
  25. foo_completed = true
  26. case sharedChan <- "done bar":
  27. fmt.Println("bar goroutine done")
  28. bar_completed = true
  29. }
  30. if foo_completed && bar_completed {
  31. completed = true;
  32. }
  33. }
  34. fmt.Println("All goroutines have completed.")
  35. }
  36.  
  37. func foo(c chan string) {
  38. c <- "starting foo"
  39. time.Sleep(1*time.Minute)
  40. c <- "done foo"
  41. }
  42.  
  43. func bar(c chan string) {
  44. c <- "starting bar"
  45. time.Sleep(2*time.Minute)
  46. c <- "done bar"
  47. }
Add Comment
Please, Sign In to add comment