var counter int var mutex sync.Mutex func incrementCounter() { mutex.Lock() counter++ mutex.Unlock() }
type Cache struct { data map[string]string mutex sync.Mutex } func (c *Cache) Get(key string) (string, bool) { c.mutex.Lock() defer c.mutex.Unlock() value, ok := c.data[key] return value, ok } func (c *Cache) Set(key string, value string) { c.mutex.Lock() defer c.mutex.Unlock() c.data[key] = value }In this example, we have a cache object that stores key-value pairs. The mutex is used to ensure that only one goroutine can access the cache at a time, preventing race conditions. The Get and Set methods use the mutex to lock and unlock the cache as needed. The package library for the go sync Mutex Lock is "sync" which is a part of the Go standard library.