The map primitive in Golang should be considered as unsafe for concurrent access. In the previous recipe, we described how to synchronize access to the resource with Mutex, which could also be leveraged with access to the map primitive. But the Go standard library also provides the map structure designed for concurrent access. This recipe will illustrate how to work with it.
Creating map for concurrent access
How to do it...
- Open the console and create the folder chapter10/recipe02.
- Navigate to the directory.
- Create the file map.go with the following content:
package main
import (
"fmt"
"sync"
)
var names = []string{"Alan", "Joe", "...