go type_error ai_generated true

panic: interface conversion: interface is nil, not string (in goroutine)

ID: go/goroutine-racy-interface-conversion

Also available as: JSON · Markdown · 中文
80%Fix Rate
84%Confidence
0Evidence
2025-07-14First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.0 active
1.20 active

Root Cause

Goroutine attempts type assertion on a nil interface without checking, causing panic.

generic

中文

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

Workarounds

  1. 95% success Use comma-ok type assertion
    if s, ok := i.(string); ok {
        fmt.Println(s)
    } else {
        log.Println("i is not string")
    }
  2. 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:

  1. Using recover() to catch panic 90% fail

    Recover works but doesn't fix the nil interface; data may be lost.

  2. Ignoring type assertion and using raw interface 80% fail

    Raw interface cannot be used as string; compilation error or runtime panic.