Modern computers mostly gain speed by running several instruction streams at once instead of running a single stream faster. A program that uses only one stream leaves a lot of that performance unused. Go gives you concurrency to use it, and concurrency brings problems you should understand well before writing your first goroutine.
What is concurrency
Concurrency is about structuring a program as a collection of independently progressing pieces. Rather than executing tasks one after another from start to finish, a concurrent program decomposes its work into parts that can make progress in overlapping time windows.
Think of a chef in a kitchen preparing multiple dishes simultaneously. While the pasta is boiling, which only takes waiting, the chef chops vegetables and stirs the sauce. The chef is never doing two things at the exact same instant: they interleave tasks to keep the total time down, and that interleaving is concurrency.
In Go, the primary unit of concurrent work is the goroutine: a lightweight, independently scheduled function. You can launch thousands of goroutines with minimal overhead, and the Go runtime handles scheduling them across the available CPU cores.
Concurrency is not parallelism
Rob Pike, one of Go's creators, put it this way: "Concurrency is about dealing with lots of things at once. Parallelism is about doing lots of things at once."
Concurrency is a design property of your program: how it structures work into independent pieces that can overlap in time.
Parallelism is a runtime property: whether those pieces actually execute simultaneously on different CPU cores.
On a single-core machine, a concurrent program is still concurrent. The operating system time-slices the CPU between goroutines, switching rapidly between them. They never run at the exact same instant, but they make progress interleaved, which from the outside looks like parallelism.
On a multi-core machine, the Go runtime can schedule goroutines on different cores, and the same concurrent program really does execute several things at once.
Single core (concurrent, not parallel):
Time ──────────────────────────────────▶
Core 0: [G1][G1][G2][G2][G1][G3][G3][G2]
Multi core (concurrent and parallel):
Time ──────────────────────────────────▶
Core 0: [G1][G1][G1][G2][G2]
Core 1: [G2][G3][G3][G1][G3]
So concurrency is something you design into your code, while parallelism is something you may or may not get at runtime, depending on the hardware. A program designed for concurrency runs in parallel on a multi-core machine without any change.
Why concurrency matters
A program that performs all its work on a single goroutine uses exactly one CPU core, no matter how many cores the machine has, and the rest sit idle.
With concurrency, a program can keep all available cores busy. A web server that handles each incoming request in its own goroutine can serve thousands of simultaneous clients, where a sequential server would queue them and slow to a crawl. A data pipeline that processes independent records concurrently can finish in a fraction of the time.
Concurrency also improves responsiveness. A user interface that performs a network request in a goroutine stays interactive while waiting for the response. A CLI tool that downloads files concurrently finishes earlier because it doesn't block on each one.
Go was designed from the start with concurrency in mind. Goroutines are cheap enough to use liberally, and channels give goroutines a structured way to communicate. Before using either, you need to see where the problems come from.
The challenge: race conditions
Things can go wrong as soon as two or more goroutines access the same data. A race condition happens when the correctness of a program depends on the relative timing or interleaving of goroutines, and nothing guarantees that timing.
Consider this program:
package mainimport ( "fmt" "sync")func main() { counter := 0 var wg sync.WaitGroup for i := 0; i < 1000; i++ { wg.Add(1) go func() { defer wg.Done() counter++ }() } wg.Wait() fmt.Println(counter)}The program launches 1,000 goroutines, each incrementing a counter, and prints the final value. You would expect 1000. Run it a few times and you get different values, often less than 1,000.
The result is wrong because counter++ is not a single, indivisible step, which brings us to atomicity.
Atomicity
An operation is atomic if it appears to the rest of the system as a single, instantaneous step: it either happens completely or not at all, with no observable intermediate state.
The expression counter++ looks like one thing, but it decomposes into three distinct machine-level operations:
- Read the current value of
counterfrom memory - Add 1 to that value
- Write the new value back to memory
On a single goroutine, those three steps happen in sequence with no interruption, so the result is always correct. With multiple goroutines, the Go scheduler can interleave these steps in any order. Here is one problematic scenario with two goroutines:
Goroutine A reads counter: 42
Goroutine B reads counter: 42 ← B reads before A writes
Goroutine A adds 1, writes: 43
Goroutine B adds 1, writes: 43 ← B overwrites A's result
Final counter: 43, not 44
Both goroutines read the same value, both computed 42 + 1 = 43, and both wrote back 43, so one increment was lost without any error. Across 1,000 goroutines this can happen hundreds of times, which is why the final count keeps coming out lower than expected.
Databases handle this with transactions: a group of operations that are guaranteed to execute atomically. Either all of them succeed, or none of them take effect. Go has its own tools for atomicity (the sync/atomic package for individual values, and sync.Mutex for protecting larger blocks of code), but you have to opt in to them explicitly, because the language does not protect you by default.
Critical sections
A critical section is any part of your code that accesses or modifies shared data, meaning data that multiple goroutines can reach at the same time. In the counter example, the single line counter++ is a critical section. In a more complex program, a critical section might span several lines: reading a value, computing a new one, writing it back, and updating related state.
Writing correct concurrent code starts with finding the critical sections. Whether code is a critical section depends on whether it touches shared mutable state, however many lines it has.
// Not a critical section: result is local, not sharedfunc square(n int) int { result := n * n return result}// Critical section: counter is shared across goroutinesfunc increment(counter *int) { *counter++ // must be protected}Critical sections must be protected: only one goroutine should execute them at a time. Code outside a critical section (work on local, goroutine-owned data) needs no protection and can run freely in parallel.
Critical sections are not always obvious. A function call might reach shared state through a pointer several levels deep, and a map lookup might look read-only but panic under concurrent writes. The habit that helps most is asking, about each piece of code, "does this touch data that another goroutine can see?"
Summary
- Concurrency is how a program is structured into pieces that progress in overlapping time. Parallelism is whether those pieces run at the same instant on different cores.
- A race condition happens when a program's correctness depends on the timing or interleaving of goroutines.
counter++is a read, an add and a write, so it is not atomic. Go offerssync/atomicandsync.Mutexfor atomicity, and you have to use them explicitly.- A critical section is any code that touches shared mutable state, and only one goroutine should execute it at a time.
- Go's tools for protecting critical sections and coordinating goroutines are channels,
sync.Mutex,sync.WaitGroupand thesync/atomicpackage.