Golang recommends using channel to do communicating between goroutines. The idea of channel comes from data flow. But in some scenarios, using channel is too heavy to implement, mutex is a better solution. There are two kinds of locks in Golang, sync.Mutex
,sync.RWMutex
. The difference between Mutex and RWMutex is that RWMutex is a reader/writer mutual exclusion lock, the lock can be held by an arbitrary number of readers or a single writer, readers don’t have to wait for each other, They only have to wait for writers holding the lock.