In case the code uses the concurrent access to any resource which is considered to be unsafe for concurrent use, it is necessary to implement a synchronization mechanism to secure the access. Besides the channel usage, Mutex could be leveraged for this purpose. This recipe will show you how.
Synchronizing access to a resource with Mutex
How to do it...
- Open the console and create the folder chapter10/recipe01.
- Navigate to the directory.
- Create the file mutex.go with the following content:
package main
import (
"fmt"
"sync"
)
var names = []string{"Alan", "Joe", "Jack", "Ben",
"Ellen"...