Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction 
2.
Syntax and Example of Go Select
2.1.
Syntax 
2.2.
Example 
3.
Empty select
3.1.
Example
4.
Default case
4.1.
Example
5.
Deadlock case
5.1.
Example
6.
Random selection
6.1.
Example
7.
FAQs
8.
Key takeaways
Last Updated: Mar 27, 2024

Go Select

Author Shivam Verma
0 upvote
Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction 

The select statement in Go language is just like the switch statement. Still, the main difference between the switch statement and the select statement is that the select statement works on the principle of the wait, which means a select statement will not execute until the communication, i.e., sent or receive operation on the channel, is not done. 

Syntax and Example of Go Select

Syntax 

select{
case send-receive1: // Statement 
case send-receive2: // Statement
case send-receive3: // Statement
.......
default: // Statement
}

Example 

package main
import (
    "fmt"
    "time"
)
func fun1(channel1 chan string){
    time.Sleep(2*time.Second)
    channel1<-"From channel 1"
}
func fun2(channel2 chan string){
    time.Sleep(3*time.Second)
    channel2<-"From channel 2"
}
func main(){
    chnl1:=make(chan string)
    chnl2:=make(chan string)
    go fun1(chnl1)
    go fun2(chnl2)
    select{
    case opr1:=<-chnl1:
        fmt.Println(opr1)
    case opr2:=<-chnl2:
        fmt.Println(opr2)
    }
}

Output 

From channel 1

In the program above, the fun1 function sleeps for 2 seconds then writes the text from fun1 to the channel channel1. The fun2 function sleeps for 3 seconds and then writes from fun2 to the channel channel2. Now, the select statement waits till their sleep time. When the fun2 wakes up, it selects case2 and prints "From channel 2". If the fun1 wakes up before the fun2, the output is "From channel 1".

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Empty select

If the select statement does not have any case statement, then that select statement waits forever, resulting in a deadlock. 

Example

package main
func main(){
    select{ }
}

Output

fatal error: all goroutines are asleep - deadlock!

goroutine 1 [select (no cases)]:
main.main()
/home/UdxtgL/prog.go:3 +0x14

Default case

A select statement can also have a default case like a switch statement. This statement executes if none of the other cases are ready for execution. The default statement in the select statement prevents the select from blocking forever. 

Example

package main
import (
    "fmt"
    "time"
)
func fun1(channel1 chan string){
    time.Sleep(2*time.Second)
    channel1<-"From channel 1"
}
func fun2(channel2 chan string){
    time.Sleep(3*time.Second)
    channel2<-"From channel 2"
}
func main(){
    chnl1:=make(chan string)
    chnl2:=make(chan string)
    go fun1(chnl1)
    go fun2(chnl2)
    select{
    case opr1:=<-chnl1:
        fmt.Println(opr1)
    case opr2:=<-chnl2:
        fmt.Println(opr2)
    default:
fmt.Println("Nothing available")
    }
}

Output

Nothing available

Deadlock case

A deadlock happens when there is no case statement is ready, and the select statement does not contain any default statement.

Example

package main
func main(){
    chnl1:=make(chan string)
    select{
        case<-chnl1:
    }
}

Output

fatal error: all goroutines are asleep - deadlock!

goroutine 1 [chan receive]:
main.main()
/home/Rj62Al/prog.go:5 +0x5e

Random selection

In the select statement, if multiple cases are ready to proceed, then one of them will be executed at random.

Example

package main
import (
    "fmt"
)
func fun1(channel1 chan string){
    channel1<-"From channel 1"
}
func fun2(channel2 chan string){
    channel2<-"From channel 2"
}
func main(){
    chnl1:=make(chan string)
    chnl2:=make(chan string)
    go fun1(chnl1)
    go fun2(chnl2)
    select{
    case opr1:=<-chnl1:
        fmt.Println(opr1)
    case opr2:=<-chnl2:
        fmt.Println(opr2)
    }
}

Output

From channel 2

FAQs

  1. What is Golang's select statement?
    Ans: The select statement in Go language is just like the switch statement. It is used to choose from multiple channel operations. The select statement blocks until any of the cases provided are ready. If multiple operations are ready to proceed, they will be chosen at random.
     
  2. What are channels in the Go language?
    Ans: Channel is a technique that is used to connect concurrent goroutines. By default, the channel is bidirectional, which means the goroutines can send or receive data through the same channel.
     
  3. When does select statement blocks in Go language?
    Ans: The blocking of a select statement happens when there is no case statement is ready and the select statement does not contain any default statement.

Key takeaways

In this article, we have extensively discussed about the select in Go programming language. We also discussed some important points with the help of examples regarding Go select.

We hope that this blog has helped you enhance your knowledge regarding Go Select and if you would like to learn more, check out our article on Go Slice. You can read other C language articles by clicking here. Do upvote our blog to help ninjas grow. Happy Coding!

Live masterclass