What are goroutines? And how do they actually work?

João Henrique Machado Silva
The Polyglot Programmer
6 min readMar 14, 2019

--

One of the main reasons that the Go Language has gained incredible popularity in the past few years is the simplicity it deals with concurrency with its lightweight goroutines and channels.

Concurrency is not something necessarily new, it has existed since long ago in the form of threads which are widely used in almost all applications now a days.

But first, before actually understanding what are goroutines, and no, they are not lightweight threads (although goroutines rely upon thread to run), we are going to dig into how actual threads work in the OS.

What are Threads?

A thread is the smallest unit of processing that can be performed in an OS. In most modern operating systems, a thread exists within a process — that is, a single process may contain multiple threads.

A good example is a web server.

A webserver normally is designed to handle multiple requests at once. And these requests normally are independent from each other.

So a thread can be created, or taken from a thread pool, and requests can be delegated, to achieve concurrency. But remember from the famous Rob Pike talk, "Concurrency is not Parallelism".

But is a thread lighter then a process…

--

--