Stage 3 · Build
Concurrency in Depth
select, Timeouts & Tickers
select fairness, time.After pitfalls, ticker cleanup, and non-blocking operations.
select Fairness
When multiple select cases are ready, Go chooses one randomly. This prevents starvation — no goroutine is permanently skipped. Understanding this behavior is critical for writing correct concurrent code.
time.After Pitfalls
time.After creates a new timer each time it is called. In a loop, this leaks timers and consumes memory. Use time.NewTimer and reset instead.
Timeout Patterns
Timeouts prevent goroutines from blocking forever. The standard pattern uses context.WithTimeout or time.AfterFunc.
Ticker Patterns
Tickers fire at regular intervals. Use them for periodic tasks: health checks, metrics collection, cache refreshes. Always stop tickers to prevent leaks.
Non-Blocking Operations
Non-blocking channel operations use select with default. They attempt a send or receive and return immediately if it would block.
Deadline Patterns
Deadlines are absolute timeouts. They propagate through context chains and ensure operations complete by a specific time.
context.WithTimeout and context.WithDeadline are preferred over time.After because they integrate with the standard library, propagate through call chains, and support cancellation. Use time.After only for simple, one-shot timeouts.
Mark this lesson complete to store local progress and unlock a cleaner resume path the next time you visit.