Golang Sync

Learn via video courses
Topics Covered

Overview

The package Golang Sync is used for providing primitives for synchronization like Mutex, Waitgroup, Read and Write mutex as well as Pool and conditional variables.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Introduction to Golang Sync

The Golang Sync package is used for providing basic primitives of synchronization like mutual exclusion lock, and Waitgroup types most of which are used by low-level routines of the library. The synchronization of higher levels is done through a communication channel.

Mutex Golang

The Mutex, sync.Mutex, is used for providing a primitive of Mutex that allows the mutual exclusion of the resources that are shared in order to prevent the race condition. For instance, look at the example given below under the Mutex in golang.

Explanation:

In this example, the variable i is declared as 10, and the mutex is assigned with sync.Mutex and is locked. The variable i is incremented by a single goroutine and the mutex is unlocked.

Read and Write Mutex

The mutex sync.RWMutex is used for providing the reader as well as writer mutex that provides mutex which has methods similar to that of primitive mutex and allows concurrent read operation by using the methods Read and Write Mutex. In order to understand this better, look at the example given below:

Explanation:

In this example, the code allows a single goroutine to perform the read operation on the code. The sync.Mutex, however, allows only one reader and one writer at an instance of time to read the code.

Golang Waitgroups

The method sync.Waitgroup is used for providing the blocking mechanism for the goroutines and by using it, we can block the function execution until all the goroutines are done with the execution. The working of this method is that it creates a counter variable that stores the number of goroutines that are in the queue to wait.

When the goroutine is done with the execution, the counter variable is decreased by 1 and when the counter variable is back to 0, the Waitgroup unblocks the execution of the code. In order to add a value to the Waitgroup counter, the Add() method is used which takes an integer value as an argument. For removing the goroutine from the counter when the code is executed, the Done() method is used. To understand this better, look at the example given below:

Output:

Explanation:

In this example, the value of the Waitgroup counter is incremented by 1 by using the function Add() in the code. When the goroutine is completed, the counter is decremented by 1 by using the function Add() in the code.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data

Once Golang

The primitive sync.Once is used for ensuring that the function is executed only once. Look at the code given below to understand this better.

Output:

Explanation:

As you can see from this example, the code is executed only once because of the RunOnce function of golang and does not execute the for loop of the code.

Conclusion

  • The package Golang sync is used for providing primitives for synchronization like Mutex, Waitgroup, Read and Write mutex as well as Pool and conditional variables.
  • The synchronization of higher levels is done through a communication channel.
  • The Mutex, sync.Mutex, is used for providing a primitive of Mutex that allows the mutual exclusion of the resources that are shared in order to prevent the race condition.
  • The mutex sync.RWMutex is used for providing the reader as well as writer mutex that provides mutex which has methods similar to that of primitive mutex and allows concurrent read operation by using the methods Read and Write Mutex.
  • The method sync.Waitgroup is used for providing the blocking mechanism for the goroutines and by using it, we can block the function execution until all the goroutines are done with the execution.
  • The primitive sync.Once is used for ensuring that the function is executed only once.
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more