Welcome To Golang By Example

Menu
  • Home
  • Blog
  • Contact Us
  • Support this website
Menu

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

Posted on August 14, 2020August 14, 2020 by admin

Table of Contents

  • Overview
  • Code

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!
  • go
  • golang
  • Follow @golangbyexample

    Popular Articles

    Golang Comprehensive Tutorial Series

    All Design Patterns in Go (Golang)

    Slice in golang

    Variables in Go (Golang) – Complete Guide

    OOP: Inheritance in GOLANG complete guide

    Using Context Package in GO (Golang) – Complete Guide

    All data types in Golang with examples

    Understanding time and date in Go (Golang) – Complete Guide

    ©2025 Welcome To Golang By Example | Design: Newspaperly WordPress Theme