Go Tutorial: Concurrency
Do multiple things at once. Goroutines and channels are Go's superpower — lightweight, safe, and elegant.
Go was built for this
Most languages treat concurrency as an add-on. You reach for a library, learn a callback system, or fight with threads and locks.
Go was designed from day one with concurrency in mind. A goroutine costs less than 2KB of memory. You can run thousands of them. Channels let them communicate safely without shared memory bugs.
The famous Go proverb sums it up:
Do not communicate by sharing memory. Share memory by communicating.
This chapter is why people fall in love with Go. Fourteen steps.
What you'll learn in this Go concurrency tutorial
This interactive Go tutorial has 9 hands-on exercises. Estimated time: 28 minutes.
- What is a goroutine — A goroutine is a lightweight task Go runs alongside your main code. Start one with the `go` keyword. Run this — the sear…
- Channels — communicate between goroutines — A channel is a pipe between goroutines. One goroutine sends a result, another receives it. Run this — a goroutine looks …
- Send and receive multiple values — A channel can send as many results as you need. Start three goroutines, each finding a book by ISBN. Add a third gorouti…
- WaitGroup — wait for goroutines to finish — `sync.WaitGroup` lets you wait for a group of goroutines to complete. Run this. Three volunteers each process a shelf se…
- Buffered channels — A regular channel blocks until the receiver is ready. A buffered channel has a queue — `make(chan string, 3)` lets you s…
- Range over a channel — You can loop over a channel with `range` — it receives values until the channel is closed. `close(ch)` signals that no m…
- Mutex — protect shared data — When multiple goroutines access the same variable, use `sync.Mutex` to prevent race conditions. Run this — a library cou…
- Race condition — spot and fix it — The code below has a race condition on `count`. Run it a few times — the result varies. Fix it by adding a `sync.Mutex` …
- Build a concurrent pipeline — Build a three-stage concurrent book processing pipeline: `findBooks(ids ...int) <-chan string` sends formatted titles, `…
Go Concurrency concepts covered
- Go was built for this