Home »
Golang
Golang select statement
By IncludeHelp Last updated : October 05, 2024
Golang select statement
The select statement in Golang is also a multiway conditional statement like a switch. But it offers a special function, each case of the switch statement is a communication. Each case either contains a send operation or receive operation on the channel which is selected based on the case value. The select statement is capable of selecting any one operation to be performed on the channel.
Flow chart
Syntax
select {
case send_Rec_Op1:
// code
case send_Rec_Op2:
// code
...
...
...
default:
// code
}
Each case value is either a send or receive operation on the channel and the statement works on the communication.
Important Details:
- The number of cases in select's body has no upper limit.
- The values of each case need to be an operation from the communication channel.
- There are no break statements in the select statement but fallthrough can be implemented.
- For the statement to execute, a communication channel needs to be prepared i.e. case statements will execute after send or receive operations are ready to work.
- The condition goes into a deadlock state and waits forever when no case statement is present.
- In case when all the condition cases are ready for communication, a random case is selected randomly.
Example of select statement
Golang program to illustrate the working of the select statement
package main
import (
"fmt"
"time"
)
func port1(channel1 chan string) {
time.Sleep(3 * time.Second)
channel1 <- "Communicating through 1ST channel"
}
func port2(channel2 chan string) {
channel2 <- "Communicating through 2ND channel"
}
func main() {
chan1 := make(chan string)
chan2 := make(chan string)
go port1(chan1)
go port2(chan2)
select {
case op1 := <-chan1:
fmt.Println(op1)
case op2 := <-chan2:
fmt.Println(op2)
}
}
Output:
Communicating through 2ND channel
Go select statement with default
To specify a default action if none of the cases are ready, use the default case.
Example
Golang program to illustrate the working of the select statement with default
package main
import (
"fmt"
"time"
)
func port1(channel1 chan string) {
time.Sleep(3 * time.Second)
channel1 <- "Communicating through 1ST channel"
}
func port2(channel2 chan string) {
channel2 <- "Communicating through 2ND channel"
}
func main() {
chan1 := make(chan string)
chan2 := make(chan string)
select {
case op1 := <-chan1:
fmt.Println(op1)
case op2 := <-chan2:
fmt.Println(op2)
default:
fmt.Println("No Working Channel Found")
}
}
Output:
No Working Channel Found
In the above code, there is no call to the channel creating function that is not defined. So, both the channels could not be ready which might lead to deadlock which is avoided by using the default statement. But the default is executed even if there might be a ready in the future, which leads to wrong output.
Let's see how the above code will react if there is no default statement.
Example
Golang program to illustrate the working of the select statement
package main
import (
"fmt"
"time"
)
func port1(channel1 chan string) {
time.Sleep(3 * time.Second)
channel1 <- "Communicating through 1ST channel"
}
func port2(channel2 chan string) {
channel2 <- "Communicating through 2ND channel"
}
func main() {
chan1 := make(chan string)
chan2 := make(chan string)
// go port1(chan1)
// go port2(chan2)
select {
case op1 := <-chan1:
fmt.Println(op1)
case op2 := <-chan2:
fmt.Println(op2)
}
}
Output:
fatal error: all goroutines are asleep - deadlock!
goroutine 1 [select]:
main.main()
/tmp/sandbox3748578454/prog.go:24 +0xbb