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
-
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.
-
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.
-
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!