Skip to main content

intersect chanels Intersect channels in goGo

Tweeted twitter.com/StackCodeReview/status/1385745361352986624
added 20 characters in body
Source Link

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?

I have a simple golang app in which I have two background tasks that produce data,
Each of them use it's own channel to signel it produced another unit of data.
I have a single consumer that needs the data form the two other background tasks as input in a 1-to-1 ratio.
What I want to do,for the lack of a better term, is "Intersecting" the channels.
Meaning I want tp create a new channel that gets a signel every time there's a signel in one channel and then the other.
it's also worth mentioning I have a context I use to close all background gorotines for gracefull shatdown.
 

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
        }
    }
}

I wonder if there is a more elegant/idiomatic way to achive this?

I have a simple golang app in which I have two background tasks that produce data,each of them use it's own channel to signal it produced another unit of data.

I have a single consumer that needs the data form the two other background tasks as input in a 1-to-1 ratio. What I want to do,for the lack of a better term, is "Intersecting" the channels. Meaning I want to create a new channel that gets a signal every time there's a signal in one channel and then the other.

it's also worth mentioning I have a context I use to close all background gorotines to 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
        }
    }
}

I wonder if there is a more elegant/idiomatic way to achieve this

Source Link

intersect chanels in go

I have a simple golang app in which I have two background tasks that produce data,
Each of them use it's own channel to signel it produced another unit of data.
I have a single consumer that needs the data form the two other background tasks as input in a 1-to-1 ratio.
What I want to do,for the lack of a better term, is "Intersecting" the channels.
Meaning I want tp create a new channel that gets a signel every time there's a signel in one channel and then the other.
it's also worth mentioning I have a context I use to close all background gorotines for gracefull shatdown.

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
        }
    }
}

I wonder if there is a more elegant/idiomatic way to achive this?