I have a simple golang app in which I have two background tasks that produce data,
Eacheach of them use it's own channel to signelsignal it produced another unit of data.
I
I have a single consumer that needs the data form the two other background tasks as input in a 1-to-1 ratio.
WhatWhat I want to do,for the lack of a better term, is "Intersecting" the channels.
Meaning Meaning I want tpto create a new channel that gets a signelsignal every time there's a signelsignal in one channel and then the other.
it's
it's also worth mentioning I have a context I use to close all background gorotines for gracefull shatdownto gracefully shutdown.
here is the function I wrote to do it:
func intersectChans(a <-chan bool, b <-chan bool) {
for {
select {
case <-a:
select {
case <-b:
c <- true
case <-ctx.Done():
return
}
case <-ctx.Done():
return
}
}
}
func intersectChans(a <-chan bool, b <-chan bool) {
for {
select {
case <-a:
select {
case <-b:
c <- true
case <-ctx.Done():
return
}
case <-ctx.Done():
return
}
}
}
I wonder if there is a more elegant/idiomatic way to achiveachieve this?