# WARNING: DATA RACE (goroutine read)

- **ID:** `go/goroutine-race-condition-read`
- **Domain:** go
- **Category:** runtime_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

A goroutine reads a variable while another goroutine writes to it without synchronization.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| 1.21 | active | — | — |
| 1.22 | active | — | — |

## Workarounds

1. **Use sync.RWMutex for read preference** (97% success)
   ```
   var mu sync.RWMutex
var data map[string]int
read := func(key string) int {
    mu.RLock()
    defer mu.RUnlock()
    return data[key]
}
write := func(key string, val int) {
    mu.Lock()
    defer mu.Unlock()
    data[key] = val
}
   ```
2. **Use channels to communicate instead of shared memory** (95% success)
   ```
   ch := make(chan int)
go func() {
    v := <-ch
    // use v
}()
ch <- 42
   ```

## Dead Ends

- **Using a local copy of the variable** — The copy is made before the write, but the race still exists at the read point. (70% fail)
- **Adding more goroutines to distribute load** — More goroutines increase the chance of race conditions. (85% fail)
