Dev Skills
Go·Section 4 of 15 · 7 lessons · about 6 h

Golang concurrency practice problems and exercises

The part people come to Go for. Run things at the same time, safely, and stop them when you need to.

  1. 01Goroutines
    Start ten thousand concurrent things with one keyword.
    Graded challenge
  2. 02Channels and select
    Passing values between goroutines safely, and waiting on several at once.
    Graded challenge
  3. 03Mutexes and the race detector
    Sharing state between goroutines without corrupting it.
    Graded challenge
  4. 04Context
    Cancelling work that nobody is waiting for any more.
    Graded challenge
  5. 05Concurrency patterns
    Run once, wait for many, stop on the first error, cap the parallelism, and never leak a goroutine.
  6. 06The sync package in Go: every type, with examples
    Every type in `sync`, what it is for, a working example, and the mistake people make with it.
  7. 07Worker pools in Go: bounded concurrency, built step by step
    A fixed number of goroutines pulling jobs from one channel, so 10,000 tasks never turn into 10,000 open connections.

What each lesson covers

By the end of this section you will be able to do everything below. Each lesson has runnable Go examples you can edit and run in the browser.

  1. 1. Goroutines

    • Write a program that starts several goroutines and waits for all of them with sync.WaitGroup.
    • Predict what happens when main returns before its goroutines finish.
    • Fix a WaitGroup bug where Add is called inside the goroutine instead of before it.

    Remember: When main returns the program exits, and every goroutine dies with it.

  2. 2. Channels and select

    • Write a producer that sends on a channel and closes it, and a consumer that ranges over it.
    • Explain why an unbuffered send blocks until someone receives.
    • Write a select loop that reads from two channels until both are closed.

    Remember: Know the channel behaviour table cold. Most channel deadlocks and panics are a row in it.

  3. 3. Mutexes and the race detector

    • Write a struct that guards a map with a sync.Mutex.
    • Explain why a data race is undefined behaviour, not just a wrong number.
    • Fix a racy counter and prove it with go test -race.

    Remember: Without synchronisation, one goroutine has no guarantee of ever seeing another’s writes.

  4. 4. Context

    • Write a function that takes ctx first and returns ctx.Err() when it is cancelled.
    • Explain why cancelling a context does nothing unless your code checks ctx.Done().
    • Predict whether a child context made with context.WithoutCancel stops when its parent is cancelled.

    Remember: In a handler, derive from r.Context() so a disconnecting client stops your database query too.

  5. 5. Concurrency patterns

    • Write an errgroup that runs several tasks and returns the first error.
    • Write a worker pool that caps how many goroutines run at once.
    • Explain how a goroutine leaks when nobody reads from its channel, and fix it.

    Remember: Before you type go, know how that goroutine will end.

  6. 6. The sync package in Go: every type, with examples

    • Pick the right sync type for a problem: lock, wait, run once, reuse, or signal.
    • Use WaitGroup.Go, sync.OnceValue and the typed atomics from recent Go releases.
    • Name the trap in each type, such as copying a mutex or trusting sync.Pool to keep an object.

    Remember: Mutex for state, WaitGroup to wait, Once for setup, Pool for scratch memory, atomic for one value, and a channel when data moves.

  7. 7. Worker pools in Go: bounded concurrency, built step by step

    • Write a worker pool that returns results in input order and never leaks a goroutine.
    • Stop every worker on the first error or a timeout, using a context.
    • Choose between a worker pool, a semaphore and errgroup.SetLimit, and pick a worker count with a number behind it.

    Remember: Bound the work, not the goroutines: N workers, one jobs channel, and every channel closed by the side that knows it is finished.

Test yourself