
Sync Primitives
sync.Mutex, RWMutex, Once, Pool, Map, Cond, atomic operations
1What is a sync.Mutex in Go?
What is a sync.Mutex in Go?
Відповідь
A sync.Mutex (mutual exclusion lock) is a synchronization primitive that ensures only one goroutine can access a critical section at a time. Calling Lock blocks until the mutex is available, and Unlock releases it for other goroutines. Use it to protect concurrent access to shared data.
2What happens if we forget to call Unlock after Lock on a sync.Mutex?
What happens if we forget to call Unlock after Lock on a sync.Mutex?
Відповідь
Forgetting Unlock creates a potential deadlock as other goroutines will wait indefinitely to acquire the mutex. The best practice is to use defer to ensure Unlock will be called even in case of panic. Always pair Lock and Unlock in the same scope to avoid lock leaks.
3What is the main difference between sync.Mutex and sync.RWMutex?
What is the main difference between sync.Mutex and sync.RWMutex?
Відповідь
The sync.RWMutex allows multiple concurrent reads via RLock but only one exclusive write via Lock. This improves performance for read-heavy scenarios as readers don't block each other. Use RWMutex when reads are frequent and writes are rare.
Why use defer with Unlock on a sync.Mutex?
When to prefer sync.RWMutex over sync.Mutex?
+19 питань зі співбесід
Інші теми співбесід Go
Go Basics
Go Data Structures
Go Interfaces
Error Handling
Goroutines Basics
Channels
Go Modules
HTTP Server
HTTP Client
JSON Encoding
database/sql
Context Package
Testing
Concurrency Patterns
Go Web Frameworks
REST API Design
gRPC
Reflection
Memory Management
Performance Optimization
Generics
Go Design Patterns
Microservices
Security & Authentication
Docker & Containerization
Kubernetes Basics
Advanced Go
CLI Development
Опануй Go для наступної співбесіди
Отримай доступ до всіх питань, flashcards, технічних тестів, вправ code review та симуляторів співбесід.
Почни безкоштовно