Skip to content
Learn Netverks

Lesson

Step 20/36 56% through track

select-statement

Select statement

Last reviewed May 28, 2026 Content v20260528
Track mode
server_compiled
Means
Compiled runner
Reading
~1 min
Level
intermediate

This lesson

This lesson teaches Select statement: the syntax, patterns, and safety habits you need before advancing in Go.

Teams still ship Select statement in Go codebases—skipping it leaves gaps in debugging and code reviews.

You will apply Select statement in contexts like: Kubernetes ecosystem tools, cloud APIs, and CLI utilities.

Write Go in main.go with package main and func main(), click Run on server—the dev runner runs go run main.go; use fmt.Println for output (requires Go toolchain; LEARNING_RUNNER_ENABLED=true).

When you can explain the previous lesson's ideas without copying starter code.

select waits on multiple channel operations—like a switch for communication. First ready case wins; if none ready, blocks unless default provides non-blocking behavior.

Patterns

select {
case v := <-ch:
    fmt.Println(v)
case <-time.After(time.Second):
    fmt.Println("timeout")
default:
    fmt.Println("no channel ready")
}

Use select for timeouts, fan-in, and cancellation signals—common in Kubernetes controllers and network servers.

Important interview questions and answers

  1. Q: select with default?
    A: Non-blocking—runs default if no other case ready immediately.
  2. Q: Multiple cases ready?
    A: Go chooses pseudo-randomly among ready cases—do not assume priority without design.

Self-check

  1. What does select do?
  2. When use default in select?

Cancellation pattern

Combine select with context.Context done channel to abort long operations. In servers, one goroutine per request often selects on client disconnect and work completion.

Empty select{} blocks forever—sometimes used to park main in tiny demos, never in production servers.

Tip: Combine select with context.Done() for timeouts—standard in Kubernetes operators and HTTP handlers.

Interview prep

select with default?

Non-blocking when no case ready—useful for polling or avoiding deadlock.

Multiple ready cases?

Go chooses pseudo-randomly among ready cases.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • default case?
  • Nil channel block?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump