Golang | Deadlock and Default Case in Select Statement
Last Updated :
12 Jul, 2025
In Go language, the
select statement is just like a
switch statement, but in the select statement, the 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
In this article, we learn how the default case is used to avoid deadlock. But first, we learn what is a deadlock?
Deadlock: When you trying to read or write data from the channel but the channel does not have value. So, it blocks the current execution of the goroutine and passes the control to other
goroutines, but if there is no other goroutine is available or other goroutines are sleeping due to this situation program will crash. This phenomenon is known as deadlock. As shown in the below example:
Example:
C
// Go program to illustrate
// how deadlock arises
package main
// Main function
func main() {
// Creating a channel
// Here deadlock arises because
// no goroutine is writing
// to this channel so, the select
// statement has been blocked forever
c := make(chan int)
select {
case <-c:
}
}
Output:
fatal error: all goroutines are asleep - deadlock!
goroutine 1 [chan receive]:
main.main()
To avoid this situation we use a default case in the select statement. Or in other words, when the deadlock arises in the program, then default case of the select statement executed to avoid deadlock. As shown in the below example, we use a default case in the select statement to avoid deadlock.
Example:
C
// Go program to illustrate how to resolve
// the deadlock problem using the default case
package main
import "fmt"
// Main function
func main() {
// Creating a channel
c := make(chan int)
select {
case <-c:
default:
fmt.Println("!.. Default case..!")
}
}
Output:
!.. Default case..!
You are also allowed to use default case when the select statement has only nil channel. As shown in the below example, channel c is nil, so default case executed if here default case is not available, then the program will be blocked forever and deadlock arises.
Example:
C
// Go program to illustrate
// the execution of default case
package main
import "fmt"
// Main function
func main() {
// Creating a channel
var c chan int
select {
case x1 := <-c:
fmt.Println("Value: ", x1)
default:
fmt.Println("Default case..!")
}
}
Output:
Default case..!
Similar Reads
reflect.Close() Function in Golang with Examples Go language provides inbuilt support implementation of run-time reflection and allowing a program to manipulate objects with arbitrary types with the help of reflect package. The reflect.Close() Function in Golang is used to close the channel v. To access this function, one needs to imports the refl
2 min read
Select Statement in Go Language In Go, the select statement allows you to wait on multiple channel operations, such as sending or receiving values. Similar to a switch statement, select enables you to proceed with whichever channel case is ready, making it ideal for handling asynchronous tasks efficiently.ExampleConsider a scenari
4 min read
Select Statement in Go Language In Go, the select statement allows you to wait on multiple channel operations, such as sending or receiving values. Similar to a switch statement, select enables you to proceed with whichever channel case is ready, making it ideal for handling asynchronous tasks efficiently.ExampleConsider a scenari
4 min read
Select Statement in Go Language In Go, the select statement allows you to wait on multiple channel operations, such as sending or receiving values. Similar to a switch statement, select enables you to proceed with whichever channel case is ready, making it ideal for handling asynchronous tasks efficiently.ExampleConsider a scenari
4 min read
reflect.Select() Function in Golang with Examples Go language provides inbuilt support implementation of run-time reflection and allowing a program to manipulate objects with arbitrary types with the help of reflect package. The reflect.Select() Function in Golang is used to executes a select operation described by the list of cases. To access this
2 min read
Combining Conditional Statements in Golang Go is a open-source programming language developed at Google by Robert Griesemer, Rob Pike, and Ken Thompson. Go is similar to C syntactically but with CSP style concurrency and many features of other robust programming language. Often refereed to as Golang because of the domain name, this language
2 min read