Chapter 9. Leveraging Go concurrency
This chapter covers
- Understanding concurrency and parallelism principles
- Introducing goroutines and channels
- Using concurrency in web applications
One of the things that Go is well known for is its ability to make writing concurrent programs easier and less susceptible to errors. This chapter introduces concurrency and discusses Go’s concurrency model and design. We’ll talk about the two main features of Go that provide concurrency: goroutines and channels. You’ll see an example of using Go concurrency in a web application to improve the app’s performance.
Concurrency is when two or more tasks start, run, and end within the same period of time and these tasks can potentially interact with each other. The tasks are considered to be concurrent to each other, as opposed to being sequential. Concurrency is a large and complex topic, and this chapter gives only a simple introduction.
A concept that’s similar but distinctly different is parallelism. It’s easy to get confused because in both cases multiple tasks can be running at the same time. In concurrency, the tasks don’t necessarily need to start or end together—their execution overlaps. These tasks are scheduled and often (though not necessarily) communicate to share data as well as coordinate the execution times.