Skip to content
Latchkey

Go "X redeclared in this block" - Fix Duplicate Declarations in CI

An identifier is declared twice in the same scope, or a short variable declaration (:=) introduces no new names. Go rejects both because each name may be declared only once per block.

What this error means

The build fails with X redeclared in this block (with the earlier declaration’s location) or no new variables on left side of :=. It is deterministic and points at the redeclaration.

go build output
./handler.go:14:6: result redeclared in this block
	./handler.go:9:6: other declaration of result
# or
./handler.go:20:9: no new variables on left side of :=

Common causes

The same name declared twice in a scope

Two var/func/const declarations or a duplicated := use the same identifier in one block, which Go forbids.

A := where every name already exists

:= requires at least one new variable on its left. If all names are already declared, Go reports no new variables on left side of := - use = instead.

How to fix it

Rename or remove the duplicate

Give the second declaration a distinct name, or drop it if it is accidental.

Go
result := compute()
other := computeOther()   // not: result := computeOther()

Use = when no new variable is introduced

When the names already exist, assign with = rather than redeclaring with :=.

Go
value, err := first()
value, err = second()     // reuse, not redeclare

How to prevent it

  • Keep variable names distinct within a scope.
  • Use := only when introducing at least one new variable.
  • Run go vet ./... to catch shadowing and reuse issues.

Frequently asked questions

What causes ""redeclared in this block""?
Two var/func/const declarations or a duplicated := use the same identifier in one block, which Go forbids.
How do I fix "redeclared in this block"?
Give the second declaration a distinct name, or drop it if it is accidental.

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card