Select

Select is a control structure in Go analogous to a communications switch statement. Each case must be a communication, either send or receive.

var c1, c2 chan int;
select {
case v :=

Select is a control structure in Go analogous to a communications switch statement. Each case must be a communication, either send or receive.

var c1, c2 chan int;
select {
case v := <-c1:
fmt.Printf("received %d from c1\n", v)
case v := <-c2:
fmt.Printf("received %d from c2\n", v)
}

Select executes one runnable case at random. If no case is runnable, it blocks until one is. A default clause is always runnable

Random bit generator

Silly but illustrative example.

c := make(chan int);
go func() {
for {
fmt.Println(<-c)
}
}();
for {
select {
case c <- 0: // no stmt, no fall through
case c <- 1:
}
}
Prints 0 1 1 0 0 1 1 1 0 1 …

Related posts:

  1. Channels in Go
  2. Concurrency
  3. Multiplexing

Leave Your Response

* Name, Email, Comment are Required