Go uses a concurrency model based on communicating sequential processes (CSP): you run work in lightweight goroutines scheduled by the Go runtime, and you coordinate them by passing values over channels instead of sharing memory under locks. Compared with OS threads in Java or C++, goroutines are far cheaper to create, and compared with Python threads they can run in parallel on several cores.
The Go slogan sums it up: do not communicate by sharing memory, share memory by communicating. Go still ships mutexes and atomics for the cases where shared state is the simpler tool.
A goroutine is a function started with the go keyword. The runtime multiplexes many goroutines onto a small pool of OS threads (the M:N model, built from goroutines G, threads M and processors P). A goroutine starts with a small stack that grows and shrinks on demand, so programs routinely run tens or hundreds of thousands of them.
The scheduler is cooperative at well-defined points (channel operations, blocking system calls, function calls) and, since Go 1.14, also preempts long-running loops asynchronously. The number of goroutines that execute Go code at the same time is limited by GOMAXPROCS, which defaults to the number of available CPUs. Since Go 1.25 it also respects container CPU limits on Linux.
Since Go 1.22 each loop iteration has its own i, so the closure above is safe without copying the variable. The output order is not deterministic, because the three goroutines run concurrently. Go 1.25 adds wg.Go(func() { ... }), which combines Add, the go statement and Done in one call.
A channel is a typed, thread-safe conduit between goroutines. An unbuffered channel makes sender and receiver meet (a synchronisation point), while a buffered channel lets the sender continue until the buffer is full.
The producer closes the channel when it is done, so the range loop ends. Only the sender should close a channel. The select statement waits on several channel operations at once, which is how you add timeouts and cancellation.
This prints gave up: context deadline exceeded. In real code, make the worker also watch ctx.Done() so it does not leak after the caller gives up.
Channels are best for handing off ownership of data and for pipelines. For protecting a counter or a cache, a sync.Mutex, sync.RWMutex or sync/atomic is simpler and faster.
Run your tests with go test -race to catch data races. The race detector is one of the most valuable tools in the Go toolchain.
Production Go code passes a context.Context as the first argument to anything that can block. The golang.org/x/sync/errgroup package runs a group of goroutines, returns the first error and cancels the shared context, which replaces much hand-written channel plumbing.
| Aspect | Go | Python | Java | C++ |
|---|---|---|---|---|
| Unit of concurrency | Goroutine | Thread, asyncio task, process | Platform or virtual thread | std::thread, std::jthread, coroutines |
| Scheduling | Go runtime, M:N | OS threads; event loop for asyncio | OS threads; JVM schedules virtual threads (Java 21+) | OS threads |
| Parallel CPU work | Yes, built in | Limited by the GIL in the default build; free-threaded builds exist since 3.13 as an option | Yes | Yes |
| Communication | Channels, plus mutexes | Queues, locks, futures | Queues, locks, CompletableFuture | Mutexes, condition variables, futures |
| Cost of a unit | Small, growable stack | Thread stack; tasks are cheap | Virtual threads are cheap | Thread stack, typically megabytes reserved |
| Function colouring | None | async and await split code | None with virtual threads | co_await for coroutines |
Java 21 virtual threads and Go goroutines solve the same problem: a blocking call in simple sequential code that scales to many tasks. Python's asyncio achieves high concurrency for I/O on one thread, but CPU-bound code needs multiprocessing or a free-threaded interpreter. C++ gives the most control and the most responsibility: you manage lifetimes, synchronisation and memory yourself.
context.select but surprising elsewhere.errgroup.SetLimit.sync.Mutex or sync.WaitGroup after first use is a bug. Pass them by pointer.No. Goroutines are managed by the Go runtime and multiplexed onto OS threads. They start with a small stack and are much cheaper to create and switch than threads.
No. Concurrency is structuring a program as independent tasks, and parallelism is running them at the same time. Go gives you concurrency, and the runtime uses multiple cores to run goroutines in parallel when GOMAXPROCS is above 1.
No. Use channels to transfer data or signal events, and use a mutex to protect shared state. Pick the one that makes the code easier to reason about.
Go has no way to kill a goroutine from outside. Pass a context.Context or a done channel and have the goroutine return when it is cancelled.
Go's model is goroutines for cheap concurrent tasks, channels and select for coordination, and context for cancellation. It avoids the thread cost of Java and C++ and the single-interpreter limits of classic Python, while still offering mutexes when shared memory is the better fit. Always run go test -race and give every goroutine a clear exit path.