How do you handle concurrent reads and writes to a map in Go?
Go’s maps are not thread-safe. Use sync.Mutex or sync.RWMutex for manual locking, or use sync.Map for a built-in thread-safe map implementation.
What is sync.Map in Go?
A sync.Map is a concurrent map provided by Go’s standard library. It is designed for safe concurrent use by multiple Goroutines without additional locking.
How does sync.Map differ from a regular map?
sync.Map includes built-in synchronization mechanisms, so you don’t need to use explicit locks. It also provides methods like Load, Store, Delete, and Range for common operations.
What is the purpose of sync.RWMutex in Go?
sync.RWMutex is used for fine-grained locking, allowing multiple Goroutines to read simultaneously but only one Goroutine to write at a time.
How do you use a sync.Mutex in Go?
To use sync.Mutex, you lock it before accessing shared data and unlock it afterward.
var mu sync.Mutex mu.Lock() // Access shared resource mu.Unlock()
What is the difference between sync.Mutex and sync.RWMutex?
sync.Mutex allows only one Goroutine to access a critical section, while sync.RWMutex lets multiple Goroutines read simultaneously but restricts write access to one Goroutine at a time.
How do you iterate over a sync.Map in Go?
Use the Range method to iterate over all key-value pairs in a sync.Map.
var sm sync.Map
sm.Store("key1", "value1")
sm.Range(func(key, value interface{}) bool {
fmt.Println(key, value)
return true // continue iteration
})What is the context package used for in Go?
The context package is used for managing deadlines, cancellation signals, and carrying request-scoped data across API boundaries.
How do you create a context with a timeout in Go?
Use context.WithTimeout.
ctx, cancel := context.WithTimeout(context.Background(), time.Second) defer cancel()
How do you detect deadlocks in Go?
Deadlocks occur when Goroutines wait indefinitely for resources. They can be detected using Go’s built-in race detector by running your code with go run -race or go test -race.
What is the difference between buffered and unbuffered channels in Go?
Buffered channels have a capacity and do not block the sender until the buffer is full. Unbuffered channels block the sender until the receiver is ready.
and why would you do it?How do you close a channel in Go
Use the close function to close a channel. Closing a channel signals that no more values will be sent, allowing receivers to terminate gracefully.
What happens when you try to send to a closed channel in Go?
Sending to a closed channel causes a panic.
What is the purpose of Go’s runtime.Gosched() function?
runtime.Gosched() yields the processor, allowing other Goroutines to run. It does not block the current Goroutine.
What is the purpose of the sync.Map in Go?
sync.Map is a concurrent map that is safe for use by multiple Goroutines. It provides built-in synchronization for read and write operations.
How do you use sync.Map in Go?
```
var m sync.Map
m.Store(“key”, “value”)
value, ok := m.Load(“key”)
m.Delete(“key”)
~~~
What is the purpose of the context package in Go?
The context package is used to carry deadlines, cancellation signals, and other request-scoped values across API boundaries and between processes.
How do you create a context with a timeout in Go?
```
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
~~~
What is the purpose of the context.WithCancel function in Go?
context.WithCancel returns a copy of the parent context that is canceled when the returned cancel function is called.
How do you use context.WithCancel in Go?
```
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
~~~
What is the purpose of the context.WithValue function in Go?
context.WithValue returns a copy of the parent context that includes the provided key-value pair.
How do you use context.WithValue in Go?
```
ctx := context.WithValue(context.Background(), “key”, “value”)
~~~
What is the purpose of the context.Background function in Go?
context.Background returns a non-nil, empty context. It is the top-level context for incoming requests.
What is the purpose of the context.TODO function in Go?
context.TODO is used when it’s unclear which context to use or if the function has not been updated to accept a context.