var mu sync.RWMutex var counter int func writer() { mu.Lock() counter++ mu.Unlock() } func reader() { mu.RLock() fmt.Println(counter) mu.RUnlock() }
var mu sync.RWMutex var myResource []int func writer() { mu.Lock() myResource = append(myResource, 1) mu.Unlock() } func reader() { mu.RLock() fmt.Println(myResource) mu.RUnlock() }In the above example, the writer function acquires the write lock and appends a value to the slice while the reader function acquires the read lock to read the entire slice without blocking other readers. In conclusion, the sync package library provides RWMutex and its methods for readers-writer locks to control access to shared resources in a concurrent environment.