Goroutines - Concurrency in Golang

Learn via video courses
Topics Covered

Overview

A goroutine is a function that runs concurrently with certain other goroutines; it is a lightweight thread. Using the channel operator, we can transport values through channels. As a result, a channel provides a way for the goroutine to communicate with one another without using locks. The channel is bidirectional, meaning that data can be sent and received through the same channel.

Introduction

Go language provides us with a special feature known as a goroutine in golang. It is a light weighted thread. Goroutine is a function or a method that executes simultaneously with other goroutines in the program. Each program contains a single goroutine known as the main goroutine. All the goroutines are connected to the main goroutine, if the main goroutine is terminated then all other goroutines are also terminated. Goroutines work in the background.

How to Create a Goroutine?

Goroutines are created in the program by using the go keyword.

The method func is used to define a function, and the go prefix is used to call the function to run as a goroutine.

Output:

A function is created goroutine1 then it is called in two ways. First, as a normal function and as a goroutine. The result of the function is displayed only. The result of the goroutine is not displayed because the goroutine call returns immediately when a new goroutine is executed. The control waits for the normal function to complete its execution and then moves to the next line. But when a goroutine is called the control does not wait for the completion of the goroutine it goes to the next line ignoring the value returned by the goroutine.

Advantages of Goroutine

  • Goroutines are less expensive than threads.
  • Channels act as a medium for communication between two goroutines. Channels are designed to prevent race conditions when using shared memory using goroutine.
  • Goroutines are stored in a stack, whose size is flexible and can increase and decrease depending on the program. Whereas, the size of the stack in a thread is fixed.
  • Assume a program has a single thread with many Goroutines attached to it. If any of the Goroutines blocks the thread due to resource constraints, the remaining Goroutines will assign to a newly created OS thread.

How to Start a Goroutine?

The keyword go is prefixed to a method or function to initiate a goroutine. There is a difference when executing a function and a goroutine. When running a function

The following events will take place

  1. At first statement 1 is executed.
  2. The function is executed.
  3. After the completion of the execution of the function, statement 2 is executed.

However, when running a goroutine

The following events will take place

  1. At first statement 1 will be executed.
  2. The function will be executed asynchronously.
  3. Statement 2 will be executed immediately. The control will not wait for the goroutine to complete its execution, it will straight go to statement 2 and execute it, ignoring the value returned by the goroutine.

Output:

This problem can be solved by using the method time.sleep().

Output:

In the above code, we used the method time.sleep(1 * time.Second) to make the main goroutine sleep for a second so that the goroutine hell() gets some time to execute.

Starting Multiple Goroutines

Let us consider an example in which we start two goroutines.

Output:

Both the goroutines number and alphabet run concurrently. The goroutine number initially sleeps for 400 milliseconds and then executes the number 1 then sleeps again and executes number 2 and so on. Similarly, the goroutine alphabet sleeps initially for 500 milliseconds and executes a then sleeps again then executes b, and so on. The main goroutine executes both the goroutines number and alphabet and sleeps for 9000 milliseconds and then it terminates.

Anonymous Goroutine

An anonymous goroutine can be created using the prefix go. The anonymous goroutines do not need any name for being declared.

Output:

In the above code, we have a main goroutine under which we have created an anonymous goroutine. Being an anonymous goroutine it does not need any name for being executed.

Conclusion

Hello developer!! I am sure by now you must have understood what goroutines in golang is. Let us summarize what we have learned so far

  • A channel acts as a medium that enables the goroutine to communicate amongst each other and the communication is lock-free.
  • Goroutine is a light weighted thread, it is a function that executes concurrently with some other goroutines.
  • The keyword go is prefixed to a method or function to initiate a goroutine.
  • The anonymous goroutines do not need any name for being declared.