# 警告：数据竞争：协程X写入，协程Y读取（通道发送/接收）

- **ID:** `go/goroutine-racy-channel-send-recv`
- **领域:** go
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

通过通道发送结构体指针，然后发送者在接收者读取时修改结构体。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 1.0 | active | — | — |
| 1.20 | active | — | — |

## 解决方案

1. **Send value instead of pointer** (95% 成功率)
   ```
   ch := make(chan MyStruct)
go func() {
    s := MyStruct{Field: 42}
    ch <- s // copy of s
}()
s := <-ch
   ```
2. **Use mutex to protect shared struct** (85% 成功率)
   ```
   type Shared struct {
    mu sync.Mutex
    data MyStruct
}
var s Shared
ch := make(chan *Shared)
go func() {
    s.mu.Lock()
    s.data.Field = 42
    s.mu.Unlock()
    ch <- &s
}()
   ```

## 无效尝试

- **Assuming channel synchronization protects the data** — Channel synchronizes the send/recv operation, not the data pointed to. (90% 失败率)
- **Copying pointer before send** — Copying pointer doesn't deep copy the struct; still shared. (80% 失败率)
