Select Statement in Go Language - GeeksforGeeks

20 Nov, 2019
In Go language, the select statement is just like switch statement, but in the select statement, case statement refers to communication, i.e. sent or receive operation on the channel.
Syntax:
select{

case SendOrReceive1: // Statement
case SendOrReceive2: // Statement
case SendOrReceive3: // Statement
.......
default: // Statement

Important points:
Select statement waits until the communication(send or receive operation) is prepared for some cases to begin.Example:// Go program to illustrate the// concept of select statementpackage 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"    }   // main functionfunc main(){           // Creating channels   R1:= make(chan string)   R2:= make(chan string)         // calling function 1 and    // function 2 in goroutine   go portal1(R1)   go portal2(R2)     select{          // case 1 for portal 1       case op1:= <- R1:       fmt.Println(op1)          // case 2 for portal 2       case op2:= <- R2:       fmt.Println(op2)   }      }Output:Welcome to channel 1Explanation: In the above program, portal 1 sleep for 3 seconds and portal 2 sleep for 9 seconds after their sleep time over they will ready to proceed. Now, select statement waits till their sleep time, when the portal 2 wakes up, it selects case 2 and prints “Welcome to channel 1”. If the portal 1 wakes up before portal 2 then the output is “welcome to channel 2”.
Example:
// Go program to illustrate the// concept of select statementpackage 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"    }   // main functionfunc main(){           // Creating channels   R1:= make(chan string)   R2:= make(chan string)         // calling function 1 and    // function 2 in goroutine   go portal1(R1)   go portal2(R2)     select{          // case 1 for portal 1       case op1:= <- R1:       fmt.Println(op1)          // case 2 for portal 2       case op2:= <- R2:       fmt.Println(op2)   }      }
Output:
Welcome to channel 1
Explanation: In the above program, portal 1 sleep for 3 seconds and portal 2 sleep for 9 seconds after their sleep time over they will ready to proceed. Now, select statement waits till their sleep time, when the portal 2 wakes up, it selects case 2 and prints “Welcome to channel 1”. If the portal 1 wakes up before portal 2 then the output is “welcome to channel 2”.
If a select statement does not contain any case statement, then that select statement waits forever.Syntax:select{}Example:// Go program to illustrate the// concept of select statementpackage main  // main functionfunc main() {          // Select statement    // without any case   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

Syntax:
select{}
Example:
// Go program to illustrate the// concept of select statementpackage main  // main functionfunc main() {          // Select statement    // without any case   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 select statement from blocking. This statement executes when there is no case statement is ready to proceed.Example:// Go program to illustrate the// concept of select statementpackage main  import "fmt"  // main functionfunc main() {          // creating channel    mychannel:= make(chan int)   select{     case <- mychannel:             default:fmt.Println("Not found")}   }Output:Not found
Example:
// Go program to illustrate the// concept of select statementpackage main  import "fmt"  // main functionfunc main() {          // creating channel    mychannel:= make(chan int)   select{     case <- mychannel:             default:fmt.Println("Not found")}   }
Output:
Not found
The blocking of select statement means when there is no case statement is ready and the select statement does not contain any default statement, then the select statement block until at least one case statement or communication can proceed.Example:// Go program to illustrate the// concept of select statementpackage main  // main functionfunc main() {          // creating channel    mychannel:= make(chan int)          // channel is not ready    // and no default case   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
Example:
// Go program to illustrate the// concept of select statementpackage main  // main functionfunc main() {          // creating channel    mychannel:= make(chan int)          // channel is not ready    // and no default case   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 select statement, if multiple cases are ready to proceed, then one of them can be selected randomly.Example:// Go program to illustrate the// concept of select statementpackage 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"    }  // main functionfunc main() {          // Creating channels   R1:= make(chan string)   R2:= make(chan string)        // calling function 1 and    // function 2 in goroutine   go portal1(R1)   go portal2(R2)        // the choice of selection    // of case is random   select{       case op1:= <- R1:       fmt.Println(op1)       case op2:= <- R2:       fmt.Println(op2)   }}Output:Welcome to channel 2
Example:
// Go program to illustrate the// concept of select statementpackage 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"    }  // main functionfunc main() {          // Creating channels   R1:= make(chan string)   R2:= make(chan string)        // calling function 1 and    // function 2 in goroutine   go portal1(R1)   go portal2(R2)        // the choice of selection    // of case is random   select{       case op1:= <- R1:       fmt.Println(op1)       case op2:= <- R2:       fmt.Println(op2)   }}
Output:
Welcome to channel 2
Go-Control-Flow
Golang
Go Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Different ways to concatenate two strings in Golang
time.Sleep() Function in Golang With Examples
6 Best Books to Learn Go Programming Language
strings.Contains Function in Golang with Examples
Time Formatting in Golang
strings.Replace() Function in Golang With Examples
fmt.Sprintf() Function in Golang With Examples
Golang Maps
Different Ways to Find the Type of Variable in Golang
Inheritance in GoLang