All about Goroutines

What is the purpose of goroutines?

Suppose we want to make a GET request and then process the response. There are also other steps in the same block of code that are independent of the HTTP requests and have nothing to do with the response. As we are executing our code sequentially, we’ll encounter the GET request first.

Once we send the request, we wait until we receive a response from the server. We can observe the inefficiency that would result from executing the code, though it has nothing to do with the GET request. We’ve been learning the concepts of threading and parallelism to counter this obvious and common problem.

Golang handles this problem in an elegant and easy-to-understand way. We simply put the code in a separate goroutine. The goroutines will execute parallel to each other. When using the goroutine, we’ll make a GET request that will execute independently of the current function, and the code that has nothing to do with HTTP requests can execute dependently.

Let’s walk through the code without the use of goroutines.

Get hands-on with 1200+ tech skills courses.