# 恐慌：接口转换：接口为nil，不是string（在协程中）

- **ID:** `go/goroutine-racy-interface-conversion`
- **领域:** go
- **类别:** type_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

协程在未检查的情况下对nil接口进行类型断言，导致恐慌。

## 版本兼容性

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

## 解决方案

1. **Use comma-ok type assertion** (95% 成功率)
   ```
   if s, ok := i.(string); ok {
    fmt.Println(s)
} else {
    log.Println("i is not string")
}
   ```
2. **Check for nil before type assertion** (90% 成功率)
   ```
   if i != nil {
    if s, ok := i.(string); ok {
        fmt.Println(s)
    }
}
   ```

## 无效尝试

- **Using recover() to catch panic** — Recover works but doesn't fix the nil interface; data may be lost. (90% 失败率)
- **Ignoring type assertion and using raw interface** — Raw interface cannot be used as string; compilation error or runtime panic. (80% 失败率)
