Updated May 24, 2023
Introduction to Golang Select
The Golang Select has an important conditional statement called a select statement. The select statement allows us to select or execute one expression out of many expressions, just like a switch statement in the other programing language. Still, the main difference between switch and select is that the select works on the principle of the wait, which means a select statement will only execute once the communication is done. Here, communication means a send and receives over any given channel; once the communication is done, the next check will be done, so we can understand the go language’s select statement is completely based on the channel.
Syntax
Below is the simple syntax for the select statement in the go language. We can explain the below syntax in the following points.
select{
case send-receive 1: // expression 1
case send-receive 1: // expression 2
case send-receive 1: // expression 3
.......
default: // default expression
Explanation: Select is an inbuilt statement in the go language, and we are not required to import any package to use the feature. After the select statement, we pass the various send receive, which means each of them is one kind of goroutine, and we know that the goroutine works on the principle of send and receive. Only when the one sends receive will it not be done; it will not call other send to receive statements. Once it successfully matches the case, it will execute the individual expression; else, if none of them matches, then the default expression will be executed.
How do Select Statements Work in Go Language?
Before understanding the working of the select in the go language, we need to understand its uses of it; think about what will happen if we will have many expressions and we want to execute one out of any expression on a conditional basis. So we can perform such operations with the help of the select statement in the go language. We can explain some important points to explain the working of the select statement in the go language.
Select is available in the go language without importing any package. Once we write the select, it will expect cases, and each case will perform some send-receive operation and check for the conditions. This process will wait until one communication does not complete, which means sending and receiving over one channel. Once the case is successful, it will execute the statement given inside that particular case. If none of the given cases is successful, the default statement will be executed.
Examples of Golang Select
Below, we have given some important examples of the select statement for the go language; we have granted criteria where we select one condition out of multiple statements, allowing us to handle various situations. From examples, we can see that we are passing the conditions, and it works based on the send and receive pattern, which means inside the select statement, until the one statement does not complete the process of sending and receiving, it would not allow others to start executions. If we want to test the examples, we can create a file with the name select.go and copy and paste the examples below on the file and run the command go run select.go, and we can get the output of the expression.
Example #1
Please see the below example, along with the screen of the output.
Code:
//Starting the import of the required package for the program
package main
import "fmt"
import "time"
func test1(ch1 chan int) {
time.Sleep(1*time.Second)
ch1 <- 2
}
func test2(ch2 chan int) {
time.Sleep(2*time.Second)
ch2 <- 1
}
func main(){
//Creating channel of string type , we are passing chan as the int
c1:= make(chan int)
c2:= make(chan int)
go test1(c1)
go test2(c2)
//Here we are calling two functions as the goroutine , where next call be made when send and receive has been done for others.
select{
case operation1:= <- c1:
fmt.Println("The operation1 value is",operation1 ,"And it is the channel number")
case operation2:= <- c2:
fmt.Println("The operation1 value is",operation2,"And it is the channel number")
}
}
Output:
Example #2
Please see the below example, along with the screen of the output.
Code:
//Starting the import of the required package for the program
package main
import "fmt"
func main() {
//Creating channel of string type, we are passing chan as the string
channel:= make(chan string)
//Starting selection process which will work on the basis of the send and receive pattern
select{
case <- channel:
default:fmt.Println("The channel does not exists")
}
}
Output:
Example #3
Please see the below example, along with the screen of the output.
Code:
//Starting the import of the required package for the program
package main
import "fmt"
func test1(ch1 chan int){
for k := 1; k <= 4; k++{
ch1 <- 5
}
}
func test2(ch2 chan int){
ch2 <- 6
}
func main() {
//Creating channel of string type , we are passing chan as the int
c1:= make(chan int)
c2:= make(chan int)
//Here we are calling two functions as the goroutine , where next call be made when send and receive has been done for others.
go test1(c1)
go test2(c2)
//Starting selection process which will work on the basis of the send and receive pattern
select{
case operation1:= <- c1:
fmt.Println("The value of the operation 1 is ",operation1 ,"And this is the channel number")
case operation2:= <- c2:
fmt.Println("The value of the operation 2 is ",operation2 ,"And this is the channel number")
}
}
Output:
Conclusion
From the above tutorial, we have learned the basic concept of the select in the go language; we learned about the syntax of select. We saw and understood the working of the select in the go language, and we also focused on some of the important examples of the go language, which explains more about its working of it.
Recommended Articles
We hope that this EDUCBA information on “Golang Select” was beneficial to you. You can view EDUCBA’s recommended articles for more information.