In my case, I have thousands of goroutines working simultaneously as work()
. I also had a sync()
goroutine. When sync
starts, I need any o
I edited jimt's excellent example to not loop around when it reaches default case and if state == Paused.
65 default:
66 fmt.Printf("Default case\n")
67 if state == Paused {
68 fmt.Printf("Default state Paused\n")
69 select {
70 case state = <-ws:
71 switch state {
72 case Running:
73 fmt.Printf("Worker %d: Running\n", id)
74 break
75 case Stopped:
76 fmt.Printf("Worker %d: Stopped\n", id)
77 return
78 }
79 break
80 }
81 }
82 fmt.Printf("Do actual work here\n")
An extra select+case, combination will be required to avoid a forever loop.