go
type_error
ai_generated
true
恐慌:接口转换:接口为nil,不是string(在协程中)
panic: interface conversion: interface is nil, not string (in goroutine)
ID: go/goroutine-racy-interface-conversion
80%修复率
84%置信度
0证据数
2025-07-14首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 1.0 | active | — | — | — |
| 1.20 | active | — | — | — |
根因分析
协程在未检查的情况下对nil接口进行类型断言,导致恐慌。
English
Goroutine attempts type assertion on a nil interface without checking, causing panic.
解决方案
-
95% 成功率 Use comma-ok type assertion
if s, ok := i.(string); ok { fmt.Println(s) } else { log.Println("i is not string") } -
90% 成功率 Check for nil before type assertion
if i != nil { if s, ok := i.(string); ok { fmt.Println(s) } }
无效尝试
常见但无效的做法:
-
Using recover() to catch panic
90% 失败
Recover works but doesn't fix the nil interface; data may be lost.
-
Ignoring type assertion and using raw interface
80% 失败
Raw interface cannot be used as string; compilation error or runtime panic.