March 2019
Beginner to intermediate
324 pages
7h 17m
English
In Go, a simple lock is a pointer to a mutex struct type, which belongs to the sync package. We can create a mutex as follows:
var myMutex = &sync.Mutex{}
Let's assume that we have a map called myMap of the map[int]int type that we'd like to protect from the concurrent access of multiple goroutines:
myMutex.Lock()myMap[1] = 100myMutex.Unlock()
If we ensure that all goroutines that need to edit myMap have access to myMutex, we can protect myMap against multiple goroutines changing it at the same time.
Read now
Unlock full access