syncaccess
About 254 wordsLess than 1 minute
2025-01-16
Detects potential data races and synchronization issues.
Category
Safety
What It Checks
This analyzer detects:
- Unsynchronized access to shared variables
- Missing mutex locks
- Potential race conditions
Why It Matters
Data races cause:
- Corrupted data
- Unpredictable behavior
- Hard-to-reproduce bugs
- Security vulnerabilities
Examples
Bad: Unsynchronized Counter
type Counter struct {
value int // Accessed from multiple goroutines
}
func (c *Counter) Increment() {
c.value++ // Data race!
}
func (c *Counter) Get() int {
return c.value // Data race!
}Good: Mutex Protection
type Counter struct {
mu sync.Mutex
value int
}
func (c *Counter) Increment() {
c.mu.Lock()
defer c.mu.Unlock()
c.value++
}
func (c *Counter) Get() int {
c.mu.Lock()
defer c.mu.Unlock()
return c.value
}Good: Atomic Operations
type Counter struct {
value atomic.Int64
}
func (c *Counter) Increment() {
c.value.Add(1)
}
func (c *Counter) Get() int64 {
return c.value.Load()
}Bad: Map Access
type Cache struct {
data map[string]string
}
func (c *Cache) Set(key, value string) {
c.data[key] = value // Data race!
}
func (c *Cache) Get(key string) string {
return c.data[key] // Data race!
}Good: sync.Map or RWMutex
type Cache struct {
data sync.Map
}
func (c *Cache) Set(key, value string) {
c.data.Store(key, value)
}
func (c *Cache) Get(key string) (string, bool) {
v, ok := c.data.Load(key)
if !ok {
return "", false
}
return v.(string), true
}Configuration
# .golint-sl.yaml
analyzers:
syncaccess: true # enabled by defaultWhen to Disable
- Single-threaded code
- Code protected by external synchronization
analyzers:
syncaccess: falseRelated Analyzers
- goroutineleak - Goroutine safety
- nilcheck - Nil safety
