English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In Go language, the select statement is similar toSwitch statementHowever, in the select statement, the case statement refers to communication, that is, the send or receive operation on the channel.
Syntax:
select{ case SendOrReceive1: // statement case SendOrReceive2: // statement case SendOrReceive3: // statement ...... default: // statement
Important Matters:
Select statement waits for communication (send or receive operation) to be ready, so that it can start in some cases.
package main import ( "fmt" "time" ) // function 1 func portal1(channel1 chan string) { time.Sleep(3 * time.Second) channel1 <- "Welcome to channel 1" } // function 2 func portal2(channel2 chan string) { time.Sleep(9 * time.Second) channel2 <- "Welcome to channel 2" } func main() { // Create a channel R1 := make(chan string) R2 := make(chan string) // Using goroutine to call a function1and function2 go portal1(R1) go portal2(R2) select { // case 1 case op1 := <-R1: fmt.Println(op1) // case 2 case op2 := <-R2: fmt.Println(op2) } }
Output:
Welcome to channel 1
Usage Explanation:In the above program, portal1sleeps3second, portal2sleeps9seconds, after the end of their sleep time, they will be ready to continue. Now, the select statement waits for their sleep time, when portal2wakes up, it selects case 2and outputs "Welcome to channel 1". If the portal1in portal2Woke up earlier, then it will output "Welcome to channel 2”。
If the select statement does not contain any case statements, then the select statement will wait indefinitely.
Syntax:
select{}
package main func main() { //There are no cases, it will wait indefinitely select{ } }
Output:
fatal error: all goroutines are asleep - deadlock! goroutine 1 [select (no cases)]: main.main() /home/runner/main.go:9 +0x20 exit status 2
The default statement in the select statement is used to protect the select statement from being blocked. When there are no case statements ready to be executed, this statement will be executed.
package main import "fmt" func main() { //Create a channel mychannel := make(chan int) select { case <-mychannel: default: fmt.Println("Not found") } }
Output:
Not found
The blocking of the select statement means that when there are no ready case statements and the select statement does not contain any default statements, the select statement will block until at least one case statement or communication can continue.
package main func main() { //Create a channel mychannel := make(chan int) //The channel is not ready //and there is no default statement select { case <-mychannel: } }
Output:
fatal error: all goroutines are asleep - deadlock! goroutine 1 [chan receive]: main.main() /home/runner/main.go:14 +0x52 exit status 2
In the select statement, if you are ready to handle multiple cases, you can randomly select one of them.
package main import "fmt" //function 1 func portal1(channel1 chan string){ for i := 0; i <= 3; i++{ channel1 <- "Welcome to channel 1" } } //function 2 func portal2(channel2 chan string){ channel2 <- "Welcome to channel 2" } func main() { //Create a channel R1:= make(chan string) R2:= make(chan string) //call the function using goroutine1and2 go portal1(R1) go portal2(R2) //Randomly select one select{ case op1:= <- R1: fmt.Println(op1) case op2:= <- R2: fmt.Println(op2) } }
Output:
Welcome to channel 2