Sobes.tech
Senior

What happens if you leave a select statement without cases in Go?

sobes.tech AI

Answer from AI

In Go, the select statement without cases is syntactically correct, but when executed, it blocks forever. This is because select waits for at least one case to be ready, and if there are none, there is nothing to wait for and nothing to execute — the goroutine simply hangs.

Example:

select {
// no cases
}
// execution will never continue beyond this point

Such code is usually used to block a goroutine forever, for example, to prevent a program from terminating if there is nothing else to do.