go
type_error
ai_generated
true
panic: interface conversion: interface is nil, not string (in goroutine)
ID: go/goroutine-racy-interface-conversion
80%Fix Rate
84%Confidence
0Evidence
2025-07-14First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 1.0 | active | — | — | — |
| 1.20 | active | — | — | — |
Root Cause
Goroutine attempts type assertion on a nil interface without checking, causing panic.
generic中文
协程在未检查的情况下对nil接口进行类型断言,导致恐慌。
Workarounds
-
95% success Use comma-ok type assertion
if s, ok := i.(string); ok { fmt.Println(s) } else { log.Println("i is not string") } -
90% success Check for nil before type assertion
if i != nil { if s, ok := i.(string); ok { fmt.Println(s) } }
Dead Ends
Common approaches that don't work:
-
Using recover() to catch panic
90% fail
Recover works but doesn't fix the nil interface; data may be lost.
-
Ignoring type assertion and using raw interface
80% fail
Raw interface cannot be used as string; compilation error or runtime panic.