# WARNING: DATA RACE: Write by goroutine X, Read by goroutine Y (global variable)

- **ID:** `go/goroutine-racy-global-variable`
- **Domain:** go
- **Category:** runtime_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

Multiple goroutines accessing a global variable without synchronization.

## Version Compatibility

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

## Workarounds

1. **Use sync.Mutex to protect global variable** (95% success)
   ```
   var mu sync.Mutex
var global int
func setGlobal(v int) {
    mu.Lock()
    global = v
    mu.Unlock()
}
func getGlobal() int {
    mu.Lock()
    defer mu.Unlock()
    return global
}
   ```
2. **Use atomic.Value for global state** (90% success)
   ```
   var global atomic.Value
global.Store(42)
val := global.Load().(int)
   ```

## Dead Ends

- **Using global variable with local copy** — Local copy doesn't protect global; race still on global. (90% fail)
- **Using atomic.Load/Store on non-atomic variable** — Variable must be of atomic type; int64 works, but struct doesn't. (70% fail)
