golang mutex and RWMutex example
Here is an example of how to use Mutex package main import ( "fmt" "sync" ) type Counter struct { mu sync . Mutex value int } func ( c * Counter ) Inc () { c . mu . Lock () // lock defer c . mu . Unlock () // unlock at function exit c . value ++ } func ( c * Counter ) Value () int { c . mu . Lock () defer c . mu . Unlock () return c . value } func main () { var wg sync . WaitGroup c := Counter {} // 5 goroutines incrementing 1000 times each for i := 0 ; i < 5 ; i ++ { wg . Add ( 1 ) go func () { defer wg . Done () for j := 0 ; j < 1000 ; j ++ { ...