# 警告：数据竞争：协程X写入，协程Y读取（结构体字段）

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

## 根因

多个协程在没有同步的情况下访问同一个结构体字段。

## 版本兼容性

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

## 解决方案

1. **Use mutex to protect struct field** (95% 成功率)
   ```
   type Counter struct {
    mu sync.Mutex
    val int
}
func (c *Counter) Inc() {
    c.mu.Lock()
    c.val++
    c.mu.Unlock()
}
   ```
2. **Use atomic.Value for struct** (85% 成功率)
   ```
   var v atomic.Value
v.Store(MyStruct{Field: 42})
s := v.Load().(MyStruct)
   ```

## 无效尝试

- **Using atomic operations on non-atomic types** — atomic.AddInt64 works only on int64; other types need different approach. (70% 失败率)
- **Copying struct before access** — Copy doesn't prevent race; original struct still accessed. (80% 失败率)
