Using it all - concurrent singleton
Now that we know how to create Goroutines and channels, we'll put all our knowledge in a single package. Think back to the first few chapter, when we explained the singleton pattern-it was some structure or variable that could only exist once in our code. All access to this structure should be done using the pattern described, but, in fact, it wasn't concurrent safe.
Now we will write with concurrency in mind. We will write a concurrent counter, like the one we wrote in the mutexes section, but this time we will solve it with channels.
Unit test
To restrict concurrent access to the singleton
instance, just one Goroutine will be able to access it. We'll access it using channels--the first one to add one, the second one to get the current count, and the third one to stop the Goroutine.
We will add one 10,000 times using 10,000 different Goroutines launched from two different singleton
instances. Then, we'll introduce a loop to check the...