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
Identify the type mismatch
Read the compiler error to see which type was provided and which type was expected.
- 2
Determine the relationship
Check if the types share the same underlying type and can be converted, or if they are fundamentally different.
- 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