Channels or Mutexes?
Although in go there is the awesome tool of channels to facilitate concurrent programs, sometimes using a mutex is the right thing to do.
In Concurrency in Go, the author offers a concise decision tree to determine if you should use mutexes or channels. Here is the logic translated into idiomatic go:
if performanceCriticalSection {
return USE_MUTEXES
}
if transferingDataOwnership {
return USE_CHANNELS
}
if guardingInternalStateOfStruct {
return USE_MUTEXES
}
if coordinatingMultiplePiecesOfLogic {
return USE_CHANNELS
}
return USE_MUTEXES
Of course there is more explanation of how to judge these decision points. (The most important being that just because you want a high performance program does not mean that every section is a performance critical section!)
You can play with the inputs and run the program in the playground.