Make the select statement wait for more than one channel at the same time

tarrsalah :

To quote from the The Go Programming Language Specification:

A "select" statement chooses which of a set of possible communications will proceed. It looks similar to a "switch" statement but with the cases all referring to communication operations.

How can I wait for more than one channel in a case clause, so that only when both channels return the case is executed?

example:

select {

   case <-ch1, <-ch2 ... : //wait for ch1 and ch2 
        // do something 
   case  ..
}
Gustavo Niemeyer :

There's no way to wait for multiple channels in the same select case, and there's also no way way to "fallthrough" in select cases, as the documentation describes:

You can easily do something like this via other means, though.

Without select:

This only works if c1 is guaranteed to return despite the availability of c2.

v1 := <-c1
v2 := <-c2

With looping:

This works despite the ordering, but c1 and c2 must only fire once, or it might misbehave:

var v1, v2 int
for i := 0; i < 2; i++ {
    select {
    case v1 = <-c1:
    case v2 = <-c2:
    }
}
// both v1 and v2 are set here

With goroutines:

This works either way, but you lose the ordering of results:

c3 := make(chan int, 2)
go func() { c3 <- (<-c1) }()
go func() { c3 <- (<-c2) }()

first := <-c3
second := <-c3

With sync.WaitGroup:

This works either way:

var wg sync.WaitGroup
var v1, v2 int
wg.Add(2)
go func() {
    v1 = <-c1
    wg.Done()
}()
go func() {
    v2 = <-c2
    wg.Done()
}()
wg.Wait()
// v1 and v2 are both set

And so on. There are certainly other ways. The best one depends on the details of what you're trying to achieve.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Can I SELECT more than one value from the same column

Make a distribution plot with more than one values in the same graph

Make more than one chart in same IPython Notebook cell

Is it possible for a JVM to run more than one program at the same time?

Save file for more than one user at the same time

using more than one apply-templates with the same select filter

Why use more than one equal sign in a statement with the same variable?

Fill more than one column with SELECT in statement

Allow more than one accordion to open at the same time

SQL select statement where one course more than other

How to make a alert after clicking more than one time

mysql procedure : Result consisted of more than one row with select statement

How to make a bot responding once if an user sends more than one photo at the same time?

How to replace more than one word in JavaScript string at a same time?

Allow more than one python threads to use resource at the same time

Android - How to make buttons listen more than one time

Using RAND() keyword in more than one query at the same time

How to print more than one div at the same time in same pdf?

How to scan more than one location at the same time with ClamAV

Run more than one application in the same time

SQL statement to populate more than one listbox at a time

How can I make the if statement contain more than one item?

Insert records into a table from more than one users at same time

Can I use OpenGL in more than one language at the same time?

Run more than one lein task at the same time

C program fails to execute more than one statement at the same time

how to select more than one element with almost the same xpath?

Groovy Mock the same Stream call more than one time

How to select from more than one record of the same field in a JOIN