Go: Concurrent programming
This tutorial covers
- concurrent threads of execution (goroutines),
- basic synchronization techniques (channels and locks),
- basic concurrency patterns in Go,
- deadlock and data races,
- waitgroups, timers and tickers,
- parallel computation.
Before you start, you need to know how to write basic Go programs. The resources in Go: Get started will help you come up to speed quickly with Go.
Table of contents
-
Goroutines explained
A goroutine is a lightweight thread of execution.
-
Channels explained
A channel is a mechanism for two goroutines to synchronize execution and communicate by passing values.
-
Select explained
A select statement allows you to wait for multiple send or receive operations simultaneously.
-
Data races explained
A data race is easily introduced by mistake and can lead to situations that are very hard to debug. This article explains how to avoid this headache.
-
Detect data races
By starting your application with the '-race' option, the Go runtime might be able to detect and inform you about data races.
-
Deadlock
The Go runtime can often detect when a program freezes because of a deadlock. This article explains how to debug and solve such issues.
-
Wait for goroutines
A sync.WaitGroup waits for a group of goroutines to finish.
-
Broadcast a signal on a channel
When you close a channel, all readers receive a zero value. This can be used to broadcast a signal to several goroutines on a single channel.
-
Stop a goroutine
A goroutine can stop only by returning from it's top level function. To make it stoppable, let it listen to a stop signal on a channel.
-
Timer and Ticker explained
Timers and Tickers are used to wait for, repeat, and cancel events in the future.
-
Mutual exclusion lock (mutex)
A sync.Mutex is used to synchronize data by explicit locking.
-
Efficient parallel computation
To efficiently schedule parallel computation on separate CPUs is more of an art than a science. This article gives some rules of thumb.