go type-system ai_generated true

cannot use x (variable of type T) as type U in argument

ID: go/cannot-use-as-type

Also available as: JSON · Markdown
96%Fix Rate
97%Confidence
50Evidence
2023-01-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
121 active
1 active

Root Cause

Go strict type system rejects assignment or argument because types don't match, even if they're structurally similar.

generic

Workarounds

  1. 95% success Use explicit type conversion: U(x) if the types are convertible
    Go allows conversion between compatible types like int32(x) or string(b)

    Sources: https://go.dev/ref/spec#Conversions

  2. 88% success Implement the required interface on the type
    If the target is an interface, add the missing methods to your type

Dead Ends

Common approaches that don't work:

  1. Using unsafe.Pointer to convert between types 90% fail

    Bypasses type safety; leads to memory corruption if types differ

  2. Adding type conversion without checking compatibility 55% fail

    Some conversions compile but lose data (e.g., int64 to int32)

Error Chain

Frequently confused with: