# fatal error: concurrent file writes (log file)

- **ID:** `go/goroutine-racy-file-write`
- **Domain:** go
- **Category:** io_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

Multiple goroutines writing to the same file without synchronization, causing corrupted output.

## Version Compatibility

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

## Workarounds

1. **Use a mutex to serialize file writes** (95% success)
   ```
   var mu sync.Mutex
f, _ := os.Create("log.txt")
mu.Lock()
f.Write([]byte("data"))
mu.Unlock()
   ```
2. **Use a dedicated goroutine to handle all writes via channel** (90% success)
   ```
   ch := make(chan string)
go func() {
    f, _ := os.Create("log.txt")
    defer f.Close()
    for s := range ch {
        f.WriteString(s)
    }
}()
ch <- "log entry"
   ```

## Dead Ends

- **Using os.File.Write directly without lock** — File writes are not atomic; interleaved writes corrupt data. (90% fail)
- **Using bufio.Writer without synchronization** — Bufio.Writer buffers writes but still racy when flushing. (80% fail)
