Appendix E. Concurrent programming
Concurrency is dealing with a lot of things at once. Parallelism is doing a lot of things at once.
— Rob Pike, from his Waza talk (https://go.dev/blog/waza-talk).
Concurrency may involve parallelism, but they are not the same. Concurrency is about structuring the code, while parallelism is about executing the code simultaneously.
Many goroutines can run concurrently on a single CPU through context switching, while parallelism may occur when multiple CPUs execute goroutines simultaneously. Grasping this distinction helps structure concurrent code regardless of the number of available CPUs.
We'll begin by exploring goroutines and channels. This appendix builds on concepts introduced earlier in Section 1.4; it's a good idea to review Chapter 1 to understand it fully.
WARNING
We'll often omit package and import declarations to save space and trees. If none is specified, assume the package is main. Listings can also omit the work function that we'll soon add. For full code, visit the book's GitHub repository.
E.1 Goroutines
Goroutines are lightweight and independently running concurrent functions. They can handle multiple tasks concurrently without the overhead of traditional threads. We'll examine how to start a goroutine and the differences between running sequential and concurrent code.