Go language select statement
Go language conditional statement
select is a control structure in Go, similar to the switch statement.
The select statement can only be used for channel operations; each case must be a channel operation, either a send or a receive.
The select statement listens for operations on all specified channels; once one of the channels is ready, the corresponding code block is executed.
If multiple channels are ready, the select statement randomly chooses one to execute. If none of the channels are ready, the code in the default block is executed.
Syntax
The syntax of the select statement in the Go programming language is as follows:
select {
case <- channel1:
// Code to execute
case value := <- channel2:
// Code to execute
case channel3 <- value:
// Code to execute
// You can define any number of cases
default:
// Code executed when none of the channels are ready
}
The following describes the syntax of the select statement:
- Each case must be a channel.
- All channel expressions are evaluated.
- All expressions being sent will be evaluated
- If any one of the channels can proceed, it is executed; the others are ignored.
- If multiple cases can run, select will randomly and fairly choose one to execute, and the others will not execute.
Otherwise:- If there is a default clause, it is executed.
- If there is no default clause, select will block until some channel can run; Go will not re-evaluate channels or values.
Example
select statement application demonstration:
Example
import (
"fmt"
"time"
)
func main() {
c1 := make(chan string)
c2 := make(chan string)
go func() {
time.Sleep(1 * time.Second)
c1 <- "one"
}()
go func() {
time.Sleep(2 * time.Second)
c2 <- "two"
}()
for i := 0; i < 2; i++ {
select {
case msg1 := <-c1:
fmt.Println("received", msg1)
case msg2 := <-c2:
fmt.Println("received", msg2)
}
}
}
The execution result of the above code is:
received one received two
In the above example, we created two channels, c1 and c2.
The select statement waits for data from two channels. If data is received on c1, it prints "received one"; if data is received on c2, it prints "received two".
In the following example, we define two channels and start two goroutines to receive data from these channels. In the main function, we use a select statement to make a non-blocking selection between the two channels. If neither channel has available data, the statement in the default clause is executed.
After the following example is executed, it will continuously receive data from the two channels. When neither channel has available data, it will output "no message received".
Example
import "fmt"
func main() {
// Define two channels
ch1 := make(chan string)
ch2 := make(chan string)
// Start two goroutines, each receiving data from one of the channels
go func() {
for {
ch1 <- "from 1"
}
}()
go func() {
for {
ch2 <- "from 2"
}
}()
// Use a select statement to receive data from the two channels in a non-blocking manner
for {
select {
case msg1 := <-ch1:
fmt.Println(msg1)
case msg2 := <-ch2:
fmt.Println(msg2)
default:
// If neither channel has available data, execute the statements here
fmt.Println("no message received")
}
}
}