Welcome To Golang By Example

Empty select or select with no case in Go (Golang)

Table of Contents

Overview

Select block without any case statement is empty select. The empty select will block forever as there is no case statement  to execute. We know that select statement gets blocked until any of the case statement can be executed. It is also one of the way for a goroutine to wait indefinitely. But if this empty select is put in the main goroutine then it will cause a deadlock.

Let’s see a program

Code

package main

func main() {
    select {}
}

Output

fatal error: all goroutines are asleep - deadlock!

In the above program we have empty select and hence it results in a deadlock that is why you see the output as below

fatal error: all goroutines are asleep - deadlock!