Ad Space — Top Banner

cannot use as type

Go Programming Language

Severity: Minor

What Does This Error Mean?

The Go error 'cannot use X as type Y' means you passed or assigned a value of the wrong type. Go is strictly typed — it will not automatically convert between types, even when they look similar. For example, you cannot pass an int where a float64 is expected without an explicit conversion. Fix it by converting the value to the correct type using a type conversion expression.

Affected Models

  • Go 1.0 and later
  • All Go versions
  • go build, go run, go test

Common Causes

  • Passing an integer where a float64 is required, or vice versa
  • Passing a named type (like type MyInt int) where the underlying int is expected
  • Trying to use a string where a []byte is needed, or a []byte where a string is needed
  • Passing a concrete type where an interface is expected but the type does not implement the interface
  • Using a value from one numeric type (int32) where a different numeric type (int64) is required

How to Fix It

  1. Read the error message to find the received type and the expected type. For example: 'cannot use x (type int) as type float64'.

    Note both types. You need to convert from the received type to the expected type.

  2. Apply an explicit type conversion using the syntax: ExpectedType(value). For example: float64(myInt) or int(myFloat).

    Go's type conversion syntax is just the type name followed by the value in parentheses — similar to a function call.

  3. If you need to convert a string to a []byte, use []byte(myString). To convert []byte back to string, use string(myBytes).

    This is very common when working with file I/O or network code that uses []byte.

  4. If you are using a named type (type MyInt int), you need to convert explicitly: int(myIntVar) or MyInt(regularInt).

    Go treats named types as distinct even if they have the same underlying type. This is intentional for type safety.

  5. If a type should implement an interface but does not, make sure it has all the required methods with the exact signatures the interface specifies.

    Go interfaces are satisfied implicitly. If one method is missing or has a wrong signature, the type does not satisfy the interface.

When to Call a Professional

This is a compile-time error. Go will not build until the type mismatch is resolved. The compiler tells you exactly what type it received and what type it expected. Type conversion in Go is explicit and straightforward — the fix is usually one small change.

Frequently Asked Questions

Why does Go not convert types automatically like some other languages?

Automatic type conversion can hide bugs. For example, converting a float64 to an int silently drops the decimal part. By requiring explicit conversion, Go makes sure every type change is intentional and visible in the code. This makes Go programs more predictable and easier to debug.

What is the difference between type conversion and type assertion in Go?

Type conversion changes a value from one type to another: float64(myInt). Type assertion is used with interfaces — it checks whether an interface value holds a specific concrete type: myValue.(int). Use conversion for regular types and assertion when working with interface{} or any.

I am getting this error when passing to a function — can I change the function instead?

Yes, if you own the function you can change its parameter type. But if it is a standard library function or a function from another package, converting the value at the call site is the right approach. Use explicit conversion: myFunction(float64(myIntValue)).