The previous articles covered why concurrent programming is hard: race conditions, deadlocks, livelocks and starvation. Those problems don't belong to any particular language; they follow from how programs share access to resources. Languages handle them in different ways. Go deliberately promotes one mental model, and that choice explains the design of nearly every concurrency feature in the language.
Concurrency is context-dependent
Before looking at Go's model, it helps to revisit why programs use concurrency at all, since "for performance" is only part of the answer.
Concurrency does not make work faster by itself. A concurrent program that splits work across goroutines still performs the same total computation; what changes is how that work is scheduled and interleaved. On a single CPU core, goroutines run by time-slicing, with only one active at any moment, so the total CPU time stays the same. What improves is responsiveness: while one goroutine is blocked waiting for I/O, another can run.
The first case where concurrency pays off is I/O-bound work. A web server waiting for a database query, a downloader waiting for a network response and a file processor waiting for a disk read all spend most of their time blocked, and concurrency lets the program do useful work during that wait.
The second is CPU-bound work on multi-core machines. When the Go runtime distributes goroutines across multiple OS threads, each running on its own core, the work executes in parallel. An image pipeline, a bulk data processor or a compiler can finish in a fraction of the sequential time when the work really runs in parallel.
The third is program structure. Even on a single-core machine with no I/O, concurrency can make a program easier to reason about by expressing it as independent agents that react to events and communicate results. Here the gain is clarity rather than speed.
Why traditional approaches are difficult
The classic way to write concurrent code in most languages is to share data between threads and protect it with locks. Objects own their state, mutexes guard access to that state, and threads coordinate by acquiring and releasing those locks. This model is familiar, but it does not compose well.
Mutexes themselves are fine. The trouble is shared mutable state, the root cause of every concurrency hazard covered in the previous articles. Race conditions happen because two threads reach the same data without coordination. Deadlocks happen because threads hold locks while waiting for other locks. Livelocks and starvation come from the same competition over shared resources.
In traditional object-oriented code, encapsulation hides what data a type holds, but it does not hide that the data is shared. Two goroutines can hold references to the same object, and as soon as either one calls a method that mutates state, the race begins. The more an object is shared, the more elaborate its internal locking has to be, and elaborate locking is where deadlocks tend to appear.
This model also asks you to reason about every possible interleaving of operations across all goroutines at once. That is manageable in a small program, but in a large, long-lived system with dozens of goroutines and hundreds of shared objects it quickly becomes intractable.
Go and CSP
In 1978, Tony Hoare published Communicating Sequential Processes (CSP), a formal model for concurrent computation. In CSP, processes don't share memory and coordinate access with locks; they communicate by passing messages through channels. Explicit communication replaces shared state, or at least keeps it to a minimum.
Go is directly inspired by CSP. Rob Pike, one of Go's creators, worked on CSP-influenced systems at Bell Labs before designing Go's concurrency model. The language adopts CSP's two core primitives:
- Goroutines: independent, sequential processes that run concurrently
- Channels: typed conduits through which goroutines send and receive values
The Go team sums up the philosophy in an often-quoted guideline:
"Do not communicate by sharing memory; instead, share memory by communicating."
Mutexes are still allowed: Go's sync package provides them, and they are the right tool in many situations. The guideline changes what you reach for first. When two goroutines need to coordinate, ask whether the coordination can be expressed as a message. If it can, channels make the code easier to reason about, because the communication is written out in the code instead of hidden behind a shared variable.
Goroutines
A goroutine is a function executing independently and concurrently with the rest of the program. You create one with the go keyword:
package mainimport ( "fmt" "sync")func main() { var wg sync.WaitGroup for i := 0; i < 5; i++ { wg.Add(1) go func(n int) { defer wg.Done() fmt.Printf("goroutine %d\n", n) }(i) } wg.Wait()}Each go func(...) call creates a new goroutine. The five goroutines run concurrently, so their output may appear in any order. The sync.WaitGroup ensures main waits for all of them before exiting.
Goroutines are light enough for this to work at scale. An OS thread typically starts with a 1–8 MB stack and requires a kernel context switch to schedule. A goroutine starts with roughly 2–4 KB of stack, which the runtime grows dynamically as needed, and scheduling is handled in user space by the Go runtime itself. A program that would be impractical with ten thousand OS threads can comfortably run with ten thousand goroutines.
The Go runtime uses an M:N scheduling model: it multiplexes M goroutines onto N OS threads, where N is typically the number of available CPU cores (GOMAXPROCS). The scheduler moves goroutines between threads automatically, including when a goroutine blocks on I/O.
Channels
A channel is a typed conduit for sending and receiving values between goroutines. The <- operator is both the send and receive primitive:
package mainimport "fmt"func main() { ch := make(chan string) go func() { ch <- "hello from a goroutine" // send }() msg := <-ch // receive — blocks until a value is available fmt.Println(msg)}The call <-ch blocks until a goroutine sends a value, and ch <- value blocks until a goroutine is ready to receive. Because of this blocking, a channel synchronizes goroutines as well as carrying data between them: the send and the receive happen at a coordinated moment.
Channels compose into pipelines. Each stage reads from one channel, processes the values, and writes to another:
func generate(nums ...int) <-chan int { out := make(chan int) go func() { for _, n := range nums { out <- n } close(out) }() return out}func square(in <-chan int) <-chan int { out := make(chan int) go func() { for n := range in { out <- n * n } close(out) }() return out}func main() { for result := range square(generate(2, 3, 4)) { fmt.Println(result) // 4, 9, 16 }}In this pipeline, generate feeds square and square feeds main. It is a concrete example of "sharing memory by communicating": no variable is shared between stages, and values flow through channels. Adding a third processing stage requires no changes to the others.
Channels can also fan out (one goroutine sends to many) and fan in (many goroutines send to one) for more complex coordination. A later article covers both in depth.
The select statement
select lets a goroutine wait on multiple channels at once. It works like a switch over channel operations, proceeding with whichever case is ready first:
select {case msg := <-ch1: fmt.Println("from ch1:", msg)case msg := <-ch2: fmt.Println("from ch2:", msg)}If multiple cases are ready at the same time, select picks one at random. If none are ready, it blocks.
A common use is implementing timeouts: combining a work channel with time.After ensures the program does not block indefinitely waiting for a result that may never arrive.
select {case result := <-workCh: fmt.Println("result:", result)case <-time.After(5 * time.Second): fmt.Println("timed out waiting for result")}Adding a default case makes select non-blocking: it executes default immediately if no channel is ready. This is useful for checking a channel without committing to waiting.
Go's cancellation model in the context package is built on select: a goroutine selects on both its work channel and a context's Done() channel, and stops cleanly when the parent signals cancellation. A dedicated article covers it in full.
Traditional sync primitives
Not every concurrency problem is best expressed with channels. Go's sync package provides the classical synchronization primitives for situations where shared state makes more sense:
sync.Mutexandsync.RWMutexfor mutual exclusion (covered in the deadlocks article)sync.WaitGroupfor waiting on a group of goroutines to completesync.Oncefor running initialization exactly once, regardless of how many goroutines call itsync.Mapfor concurrent-safe map access without manual lockingsync/atomicfor lock-free operations on individual numeric values
var count int64// atomic increment — no mutex needed for a single variableatomic.AddInt64(&count, 1)value := atomic.LoadInt64(&count)As a rule of thumb, use channels when your problem is about passing ownership of data or coordinating sequencing between goroutines. Use sync primitives when you have a specific shared resource that multiple goroutines need to read or update in place, and a channel would add more complexity than it removes.
Real programs often use both: channels to orchestrate high-level workflow between goroutines, and a mutex or atomic to protect a specific counter or cache that several of those goroutines update.
The pieces together
Go's concurrency model has three layers:
- A simple primitive: goroutines are cheap enough to be the basic unit of concurrent work, instead of thread pools or callbacks
- A communication-first design: channels express coordination explicitly, so the flow of data between concurrent components is readable in the code
- An escape hatch:
syncandsync/atomiccover the cases where shared state with careful locking is the cleaner answer
The design aims for concurrent programs that are easier to write correctly, as well as faster. Communication is explicit and shared state is optional, which pushes you toward code that is easier to reason about, test and debug.