I wish to have a go routine listening on two channels, blocked when both channels are drained. However, if both channels contains data, I want one to be drained before the other is handled.
In the working example below I wish all out
to be drained before exit
is handled. I use a select
-statement which doesn't have any priority order. How might I get around the problem, making all 10 out-values be handled before the exit?
package main
import "fmt"
func sender(out chan int, exit chan bool){
for i := 1; i <= 10; i++ {
out <- i
}
exit <- true
}
func main(){
out := make(chan int, 10)
exit := make(chan bool)
go sender(out, exit)
L:
for {
select {
case i := <-out:
fmt.Printf("Value: %d
", i)
case <-exit:
fmt.Println("Exiting")
break L
}
}
fmt.Println("Did we get all 10? Most likely not")
}
question from:https://stackoverflow.com/questions/11117382/priority-in-go-select-statement-workaround