Best Division

Spread Your Knowledge

  • All topics
  • About us
  • Contact us
  • Privacy policy
  • Terms of service
  • Copyright
  • Facebook
  • Twitter
  • Instagram
  • Youtube
  • LinkedIn
  • Pinterest
  • Github
  • Tumblr

© 2026 Best Division

What is the difference between Go select and switch statements?

Go
Jayant Kumar
Jayant Kumar@jayantkumar314
PublishedMar 13, 2023
UpdatedNov 17, 2026
7 min read

A switch statement picks one branch by comparing a value, a boolean condition or a type, and it is plain control flow. A select statement waits on several channel send and receive operations and runs the branch whose operation can proceed. If more than one is ready, select chooses one at random, and if none is ready it blocks until one is, or runs default when present.

Both look similar because each has case clauses, but they solve different problems. Use switch for decisions inside one goroutine, and select for coordinating goroutines, timeouts and cancellation.

Table of Contents

  • How the switch statement works
    • Type switches
  • How the select statement works
    • Rules that matter in select
  • Timeouts and cancellation with select
    • Non-blocking send and receive
  • Comparing select and switch
  • Frequently Asked Questions
    • Can I use select without channels?
    • What happens if multiple select cases are ready?
    • Does Go switch fall through to the next case?
    • How do I stop a loop from a select case?
  • Summary

How the switch statement works

Go's switch evaluates cases from top to bottom and runs the first match. There is no automatic fall-through, so you do not write break at the end of each case. Cases can hold several values, and a switch without an expression is a clean replacement for an if/else if chain.

go
text

A switch can include an initialisation statement, such as switch x := compute(); x { ... }. The keyword fallthrough forces execution to continue into the next case body, and it is rarely needed.

Type switches

A type switch compares the dynamic type of an interface value. It is the idiomatic way to branch on what a value actually is.

go

Inside each case, x has the matching concrete type. The original shorthand interface{} and any mean the same thing since Go 1.18.

How the select statement works

Every case in a select must be a channel operation, either a receive (<-ch), a receive with assignment (v := <-ch, v, ok := <-ch) or a send (ch <- v). The statement blocks until one case can proceed, then runs that case only.

go
text

The loop for range 2 needs Go 1.22 or later. On older versions write for i := 0; i < 2; i++. Each iteration blocks until a message arrives, so the output follows the order in which the channels become ready.

Rules that matter in select

  • If several cases are ready at the same moment, Go picks one using a pseudo-random choice. Never rely on source order.
  • A default case runs immediately when no channel is ready, which makes the operation non-blocking.
  • A select with no cases (select {}) blocks forever.
  • Receiving from a nil channel or sending to one blocks forever, so a case on a nil channel is never chosen. You can set a channel variable to nil to disable its case.
  • A break inside a select leaves only the select, not an enclosing for loop. Use a labelled break or return to leave the loop.

Timeouts and cancellation with select

The most common real-world uses of select are timeouts and context cancellation.

go
text

The worker handles a job, then the context deadline fires and the ctx.Done() case ends the loop. Calling time.After inside a loop creates a new timer on every iteration. For long-running loops, reuse a time.Timer or rely on the context deadline.

Non-blocking send and receive

Add default to try an operation without waiting.

go

Avoid putting default in a loop around select with nothing else, because it turns the loop into a busy wait that burns CPU.

Comparing select and switch

Featureselectswitch
PurposeWait on channel operationsBranch on a value, condition or type
Case contentsChannel sends and receives onlyAny expression, or types in a type switch
MatchingFirst ready operation, random among ready onesFirst matching case, top to bottom
BlockingBlocks until a case is readyNever blocks by itself
fallthroughNot allowedAllowed
defaultMakes it non-blockingRuns when nothing matches
Typical useTimeouts, cancellation, fan-inParsing, state handling, type checks

Frequently Asked Questions

Can I use select without channels?

No. Every case in a select has to be a channel send or receive. To branch on values or conditions, use switch or if.

What happens if multiple select cases are ready?

The runtime chooses one of the ready cases at random. This is deliberate and prevents starvation of any one channel.

Does Go switch fall through to the next case?

No. Go runs only the matching case and then leaves the switch. Use the explicit fallthrough keyword if you really want the next body to run too.

How do I stop a loop from a select case?

Use return, or a labelled break such as break loop. A plain break only exits the select.

Summary

Use switch to choose a branch from values, conditions or types, and select to wait on channel communication, including timeouts via time.After or context. Remember that select picks randomly among ready cases, blocks without default, and that break exits only the select. Together they cover most control flow in concurrent Go programs.

Share