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.
./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.
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 :=.
value, err := first()
value, err = second() // reuse, not redeclareHow 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.