package main import ( "fmt" "sync" "time" ) var counter int var rwMutex sync.RWMutex // Create a RWMutex variable func main() { wg := sync.WaitGroup{} wg.Add(10) for i := 0; i < 5; i++ { // Start 5 readers that lock RWMutex for reading go read(i) } for i := 0; i < 5; i++ { // Start 5 writers that lock RWMutex for writing go write(i) } wg.Wait() } func read(id int) { for { rwMutex.RLock() // Lock for reading fmt.Printf("Reader %d: counter is %d\n", id, counter) time.Sleep(time.Millisecond * 100) // Simulate some work rwMutex.RUnlock() // Unlock for reading } } func write(id int) { for { rwMutex.Lock() // Lock for writing counter++ fmt.Printf("Writer %d: counter is %d\n", id, counter) time.Sleep(time.Millisecond * 100) // Simulate some work rwMutex.Unlock() // Unlock for writing } }This code creates 5 readers that repeatedly read a shared counter variable using RWMutex, and 5 writers that repeatedly increment the counter variable using RWMutex. The RWMutex ensures that writers have exclusive access to the counter variable while they're writing, and that multiple readers can safely read the variable simultaneously. The sync package is the package library used in this example.