Utilizing Goroutines for Concurrency
Understand goroutines in Go and how they are used for concurrency and synchronization.
We'll cover the following...
In the modern era of computers, concurrency is the name of the game. In the years before 2005 or so, computers used Moore’s law to double the speed of a single central processing unit (CPU) every 18 months. Multiple CPU consumer systems were rare, and there was one core per CPU in the system. Software that utilized multiple cores efficiently was rare. Over time, it became more expensive to increase single-core speed, and multi-core CPUs have become the norm.
Each core on a CPU supports a number of hardware threads, and operating systems (OSs) provide OS threads that are mapped to hardware threads that are then shared between processes.
Languages can utilize these OS threads to run functions in their language concurrently instead of serially, as we have been doing in all of our code so far.
Starting an OS thread is an expensive operation, and fully utilizing the thread's time requires paying a lot of attention to what we are doing.
Go takes this to another level than most languages with goroutines. Go has built a runtime scheduler that maps these goroutines onto OS threads and switches which routine is running on which thread to optimize CPU utilization.
This produces concurrency that is easy and cheap to use, requiring less mental burden on the developer. ...