The semaphore Package

Let’s learn how we can use the semaphore package to control the access of goroutines to shared resources.

What is a semaphore package?

The last lesson of this chapter presents the semaphore package, which is provided by the Go team. A semaphore is a construct that can limit or control the access to a shared resource. As we are talking about Go, a semaphore can limit the access of goroutines to a shared resource but originally, semaphores were used for limiting access to threads. Semaphores can have weights that limit the number of threads or goroutines that can have access to a resource.

The process is supported via the Acquire() and Release() methods, which are defined as follows:

Get hands-on with 1200+ tech skills courses.