Fixing 'invalid operation: mismatched types' in Go Calculations

beginnerπŸ”· Go2026-05-27| Go (all versions), Linux/macOS/Windows

Error Message

invalid operation: x + y (mismatched types int and int64)
#go#types#casting#mismatched

Context

I hit a wall recently while writing a Go script to track file uploads. I was trying to compare 4,096 bytes read against a 2.5 GB total file size. Coming from Python or JavaScript, I expected the compiler to just "deal with it" since both values were clearly integers. Instead, the build crashed immediately with a type mismatch error.

Go's type system is uncompromising. It refuses to perform implicit type conversion (coercion), even between numeric types that look identical, like int and int64. If you try to add, subtract, or compare these variables, the compiler throws a fit:

invalid operation: x + y (mismatched types int and int64)

The Debug Process

The error usually crops up in code that looks like this snippet:

package main

import (
	"fmt"
	"os"
)

func main() {
	var count int = 10
	fileInfo, _ := os.Stat("data.csv")
	size := fileInfo.Size() // This returns an int64

	// The build fails here
	if count < size {
		fmt.Println("Processing...")
	}
}

To see what's happening under the hood, use the %T verb in fmt.Printf. It's a lifesaver when you aren't sure what a specific library function is handing back to you:

fmt.Printf("count type: %T\n", count)
fmt.Printf("size type: %T\n", size)

The output confirms the conflict:

  • count is a standard int
  • size is an int64

The Solution

Since Go won't bridge the gap for you, you must manually align the types. You have two main strategies depending on your safety requirements.

Option 1: Cast the smaller type to int64 (Recommended)

This is almost always the best move. Converting a standard int to an int64 is safe because the target container is larger. You won't risk an overflow bug.

if int64(count) < size {
	fmt.Println("Fixed with int64 cast")
}

Option 2: Cast the int64 to int

Use this sparingly. On a 64-bit system, an int is 64 bits wide, but on a 32-bit system, it’s only 32 bits. If you cast a massive int64 file size (like 5 GB) to a 32-bit int, the value will truncate. This creates logic bugs that are notoriously difficult to track down.

if count < int(size) {
	fmt.Println("Fixed with int cast")
}

The Logic of Constants

You might notice that count < 100 compiles perfectly even if count is an int64. This works because untyped constants in Go are context-aware. They adopt the type of the variable they are being compared against. The error only triggers when both sides of the operation are already fixed as specific, "typed" variables.

Verification

To verify the fix, I updated the logic and ran go run main.go. If the types align, the compiler stays silent and the binary executes. I also added a quick percentage check to ensure the floating-point math worked as expected.

// Final verified code
package main

import "fmt"

func main() {
	var processed int = 500
	var total int64 = 1024

	// Use explicit casting for mixed calculations
	percentage := float64(processed) / float64(total) * 100

	fmt.Printf("Progress: %.2f%%\n", percentage)
	
	if int64(processed) != total {
		fmt.Println("Status: Incomplete")
	}
}

Lessons Learned

  • Strictness equals safety: By forcing explicit casting, Go prevents silent overflow bugs that plague C++ or JavaScript developers.
  • Standardize on int64: When handling file sizes, database IDs, or timestamps, I now use int64 globally to minimize annoying casts.
  • Watch for return types: Standard functions like len() return an int, but os.FileInfo.Size() returns an int64. Keeping this distinction in mind prevents build errors before they start.

Related Error Notes