Cannot Use X as Type Y

cannot use x (variable of type X) as type Y in argument to funcName

Quick Answer

You are passing a value of the wrong type to a function or assigning it to an incompatible variable. Match the expected type or add a conversion.

Why This Happens

Go is statically typed and does not allow implicit type conversions. Even if two types have the same underlying representation, like type MyInt int and int, they are distinct types. You must explicitly convert between them.

The Problem

type UserID int

func getUser(id int) string {
    return "user"
}

func main() {
    var uid UserID = 42
    getUser(uid) // cannot use uid (variable of type UserID) as type int
}

The Fix

func main() {
    var uid UserID = 42
    getUser(int(uid)) // explicit conversion
}

Step-by-Step Fix

  1. 1

    Identify the type mismatch

    Read the compiler error to see which type was provided and which type was expected.

  2. 2

    Determine the relationship

    Check if the types share the same underlying type and can be converted, or if they are fundamentally different.

  3. 3

    Apply the conversion

    Use an explicit type conversion like int(x) or implement the required interface.

Bugsly catches this automatically

Bugsly's AI analyzes this error pattern in real-time, explains what went wrong in plain English, and suggests the exact fix — before your users even report it.

Try Bugsly free