Skip to content
Latchkey

Go "X redeclared in this block / other declaration of X" in CI

Go forbids two declarations of the same identifier in one scope. The compiler reports both the redeclaration and the location of the original "other declaration".

What this error means

Compilation fails with "X redeclared in this block" and a follow-up line "other declaration of X" pointing at the first definition.

go build
./parse.go:18:6: parse redeclared in this block
	./parse.go:9:6: other declaration of parse

Common causes

The same name defined twice in a package

Two functions, types, or package-level vars share a name, often after copy-paste or a merge.

A variable shadowed and redeclared in the same block

A second := or var reuses a name already declared in the same lexical block.

How to fix it

Rename or remove the duplicate

  1. Open both locations the compiler prints.
  2. Decide which definition is correct and rename or delete the other.
  3. For a merge artifact, remove the accidental copy.
parse.go
// keep one declaration; rename or delete the duplicate
func parseConfig() (*Config, error) { ... }

Reuse the existing variable instead of redeclaring

Within a block, assign with = to the existing variable rather than re-declaring it with := or var.

How to prevent it

  • Build the whole package in CI so duplicate names are caught.
  • Resolve merge conflicts carefully; duplicates often arrive that way.
  • Use distinct, intent-revealing names to avoid collisions.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →