• author: CodeDonor

Introduction

In the previous section, we looked at how our program works by fetching one URL at a time and waiting for that request to complete before moving on to the next URL. This approach can significantly increase the time it takes to fetch multiple URLs. Therefore, we concluded that we need to find a way to fetch multiple requests simultaneously.

Our solution was to use Go routines. In this article, we will explain the theory behind Go routines, how they work, and how to implement them in our program.

What is a Go Routine?

When we launch a Go program, we create an automatic Go routine. A Go routine is a lightweight thread managed by the Go runtime. It is responsible for executing each line of code inside a program one by one.

We can think of a Go routine as a small engine that exists inside our running program or process. It executes all the code within a program.

How Does a Main Go Routine Execute Code?

When we start our program, the main Go routine automatically executes all the code inside the program. It starts by creating a string of slices and iterating over each link in the slice.

Next, the main Go routine calls the checklink function, which contains an HTTP request. This function call is a blocking call, meaning that the code inside it takes some time to execute. While this function is running, the main Go routine can't do anything else.

Introduction to Go Routines

To mitigate the problem of blocking calls, we need to introduce the concept of launching additional Go routines. By launching new Go routines, we can execute several functions simultaneously.

When we call the checklink function, we put the "go" keyword in front of it. The "go" keyword tells Go to run this function inside a brand new Go routine.

When we use the "go" keyword, Go launches a new Go routine to run that function. This new Go routine is like a second engine that runs the code in our program. It starts by executing each line of the code inside the checklink function one by one.

However, the first line of code that the new Go routine executes within the checklink function is the same blocking call that we had before. Therefore, the new Go routine goes to sleep and waits for the blocking call to complete.

When a Go routine encounters a blocking call, it emits an event telling the rest of the world that it can't do anything else until the blocking call completes. At this point, control flow is passed back to the main Go routine.

The main Go routine continues to iterate over each link in the slice and launches new Go routines until the number of Go routines is equal to the number of addresses we're trying to fetch.

How to Implement Go Routines in Our Program

To add Go routines to our program, we need to add the "go" keyword in front of the function call we want to execute in a new Go routine. We can think of Go routines as small engines that execute a function and its contents simultaneously. By launching multiple Go routines, we can fetch multiple URLs simultaneously.

Conclusion

Go routines are a vital aspect of concurrent programming in Go. They allow us to execute multiple functions simultaneously and save time when executing blocking calls. In this article, we have discussed the theory behind Go routines and how to implement it in our program. Understanding the intricacies of Go routines is essential for any Go programmer who wants to develop efficient and scalable applications.

Previous Post

Using Go Routines and Channels in Your Program

Next Post

Understanding Go Routines: Concurrency vs. Parallelism

About The auther

New Posts

Popular Post