Ad Space — Top Banner

no new variables on left side of :=

Go Compiler Error

Severity: Minor

What Does This Error Mean?

This Go error means you used := (short variable declaration) but all the variables on the left side were already declared in the same scope. The fix is to use = (plain assignment) instead of :=, or introduce at least one new variable on the left side.

Affected Models

  • Go 1.x (all versions)

Common Causes

  • Using := to reassign a variable already declared in the same scope
  • Multiple return values where all variables already exist in scope
  • Forgetting that := requires at least one new variable on the left side
  • Copy-pasted code where the variable names match ones already declared
  • Shadowing intended — use := in an inner scope instead

How to Fix It

  1. Change := to = if all variables on the left are already declared.

    If x and err are both already declared in this scope, use: x, err = someFunction() not: x, err := someFunction()

  2. Introduce a new variable on the left side if you need := syntax.

    If you have result, err := someFunction() but only err is new, that is valid — Go just needs at least one new variable. The issue is when none are new.

  3. If you intended to shadow the variable in a new scope, wrap the block in curly braces.

    Wrapping code in a new block {} creates a new scope, making := valid again. Use sparingly to avoid confusing variable shadowing.

Frequently Asked Questions

What is the difference between := and = in Go?

:= declares AND assigns a new variable in one step. = assigns to an existing variable. Use := when creating the variable for the first time, = for all subsequent assignments.

Why does Go not just allow := to reassign existing variables?

Go requires at least one new variable on the left of := to prevent accidental reassignment. If := worked exactly like =, it would be easy to silently reassign a variable when you intended to create a new one.