Go Select Statement
# Go Language select Statement
[ Go Conditional Statements](#)
`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 channel to execute. If no channels are ready, the code in the `default` block is executed.
### Syntax
The syntax for the `select` statement in the Go programming language is as follows:
```go
select {
case <- channel1:
// execute code
case value := <- channel2:
// execute code
case channel3 <- value:
// execute code
// you can define any number of cases
default:
// execute code when no channels are ready
}
The following describes the syntax of the `select` statement:
* Each `case` must be a channel.
* All channel expressions are evaluated.
* All sent expressions are evaluated.
* If any channel is ready, it executes, and the others are ignored.
* If multiple cases are ready to run, `select` randomly and fairly chooses one to execute, and the others will not execute.
Otherwise:
1. If there is a `default` clause, that statement is executed.
2. If there is no `default` clause, `select` will block until a channel is ready to run; Go will not re-evaluate the channel or value.
### Example
Demonstration of `select` statement application:
## Example
```go
package main
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 output 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 both channels. If data is received from `c1`, it prints "received one"; if data is received from `c2`, it prints "received two".
In the following example, we define two channels and start two goroutines to fetch data from these channels. In the `main` function, we use the `select` statement to perform a non-blocking selection between these two channels. If neither channel has available data, the statement in the `default` clause is executed.
The following example will continuously fetch data from the two channels. When neither channel has available data, it will output "no message received".
## Example
```go
package main
import "fmt"
func main() {
// Define two channels
ch1 := make(chan string)
ch2 := make(chan string)
// Start two goroutines to fetch data from the two channels respectively
go func() {
for {
ch1 <- "from 1"
}
}()
go func() {
for {
ch2 <- "from 2"
}
}()
// Use select statement to non-blockingly fetch data from the two channels
for {
select {
case msg1 := <-ch1:
fmt.Println(msg1)
case msg2 := <-ch2:
fmt.Println(msg2)
default:
// If neither channel has available data, execute the statement here
fmt.Println("no message received")
}
}
}
[ Go Conditional Statements](#)
YouTip