I have the following scenario: A function that is called every minute and each time it is called it attempts to send data to multiple defined channels as such.
var chan1 = make(chan bool)
var chan2 = make(chan bool)
var chan3 = make(chan bool)
go func() {
for {
<-time.After(1* time.Minute)
chan1 <- true
chan2 <- false
chan3 <- true
}
}()
Then in three separate go routines each channel is read via a select
like so.
go func() {
var myVar bool
select {
case <- chan1: //or chan2, chan3 etc...
myVar = true
default:
myVar = false
}()
The issue is only chan1
receives the data from the first time based loop. So what appears to be occurring is the sending of data is blocked for all subsequent channels until the first channel is read. How would I correct this?
Use a buffered channel:
var chan1 = make(chan bool, 1)
var chan2 = make(chan bool, 1)
var chan3 = make(chan bool, 1)
Or run the send asynchronously:
go func() {
chan1 <- true
}()