Go
Redeclared in This Block Fix
One block gets one declaration of each name. Declare the same name twice in the same block - twice in one file, or once in each of two files of a package - and the build stops, with the compiler pointing at the second declaration and back at the first. This guide shows both shapes, the inner-block reuse that looks the same and is legal, and the two repairs.
Written November 28, 2026 · Tested with Go 1.27.3 · 11 min read
- The rule underneath
- The first failure: one name declared twice in one file
- The second failure: the same function in two files
- The legal look-alike: a name reused in an inner block
Written November 28, 2026
Tested with Go 1.27.3
What this guide is
The error line is total redeclared in this block, with the name swapped for whichever identifier was declared twice. It fires when a single block contains two declarations of the same name: the compiler keeps the first, rejects the second, and prints both positions so the pair can be found in one reading. The usual way to earn it is an editing accident - a declaration pasted twice, or a helper copied into a second file of the same package - but the compiler cannot tell an accident from two genuinely different things that happen to share a name, and it does not try. Either way, one block ends up with one declaration, and deciding which repair applies is the reader's call: rename one declaration, or merge the two into one.
This guide reproduces the error in its two common shapes - a duplicated package-level variable in one file, and the same function declared in two files of one package - then shows the look-alike that is not an error at all: the same name declared again inside an inner block is a new, shadowing declaration, and it compiles and runs. Two fixes follow, and a final section separates the neighboring error that shares the vocabulary but not the rule.
Each program below lives in one module - run go mod init once in an empty directory, save each program under the file name shown, and build it by that name. The cross-file failure is the one exception: it is two files, and its build command names both.
The rule underneath
The Go specification's declarations and scope section states the rule in one sentence: "No identifier may be declared twice in the same block, and no identifier may be declared in both the file and package block." Everything in this guide is that sentence applied. A declaration at the top level of a file - outside any function - sits in the package block, and the scope of a top-level identifier is the whole package block, which is why two files of one package share a single namespace for their top-level names: a function declared in two files is declared twice in one block, even though each file looks complete on its own.
The same section states the exception that makes the look-alike legal: "An identifier declared in a block may be redeclared in an inner block. While the identifier of the inner declaration is in scope, it denotes the entity declared by the inner declaration." Same name, different block - a new declaration, not a duplicate. The error is never about the name itself; it is about two declarations of it landing in one and the same block.
The first failure: one name declared twice in one file
Two package-level declarations of total, one after the other:
package main
import "fmt"
var total = 1
var total = 2
func main() {
fmt.Println(total)
}
$ go build -trimpath -o p1.bin p1_main.go
# command-line-arguments
./p1_main.go:6:5: total redeclared in this block
./p1_main.go:5:5: other declaration of total
Read the pair of lines in order. The first names the declaration the compiler rejects - line 6, the second var total - and the indented line under it names the declaration already standing: line 5, the first one, labeled the "other declaration". The column is 5 in both because the identifier sits in the same place on each line, right after var. Neither value wins: the program does not build with the second value overwriting the first, and it does not build with the first value kept. A duplicate declaration is not an assignment, and the compiler offers no tie-break - the pair itself is the fault, and removing the duplication is the whole repair. Which of the two lines reflects what the code actually meant is the question the two fixes answer differently.
The second failure: the same function in two files
Splitting a package across files does not split its namespace. Here greet is declared at the top level of two files of the same package - the kind of duplication a copy of a helper into a new file produces:
package main
import "fmt"
func greet() string {
return "hello from main file"
}
func main() {
fmt.Println(greet())
}
package main
func greet() string {
return "hello from greet file"
}
$ go build -trimpath -o p2.bin p2_main.go p2_greet.go
# command-line-arguments
./p2_greet.go:3:6: greet redeclared in this block
./p2_main.go:5:6: other declaration of greet
The presentation is the same pair in the same order: the declaration being rejected first - here the copy in p2_greet.go, at column 6, right after func - and the standing declaration second, in p2_main.go. The block named in the error is the package block, which spans both files; that the two declarations sit in different files is exactly what does not matter. This is also why the error often appears after a refactor that moved code between files: a function moved to a new file while its original stayed behind is one declaration too many, and the fix is to delete the leftover, not to rename the moved copy. Where the two bodies differ, the pair of positions is the prompt to compare them before deleting either - the compiler has already found both.
The legal look-alike: a name reused in an inner block
The same name declared again is only an error inside the same block. Declare it again inside an inner block and the inner declaration is a separate variable that shadows the outer one for the length of that block:
package main
import "fmt"
var total = 1
func main() {
fmt.Println("outer:", total)
{
total := 2
fmt.Println("inner:", total)
}
fmt.Println("outer again:", total)
}
$ go build -trimpath -o p3.bin p3_shadow.go
$ ./p3.bin
outer: 1
inner: 2
outer again: 1
The output is the scope rule made visible. Inside the braces, total denotes the inner declaration and prints 2; before and after the block, it denotes the package-level variable and prints 1, unchanged by anything the inner block did. So a duplicate-looking name is not automatically this guide's error: if the second declaration sits in an inner block - a bare block, a function body, a loop body - it is shadowing, it is legal, and whether it is wise is a readability question, not a compile error. The redeclared error requires the harder condition: two declarations in one block, with no inner block between them.
First fix: rename one declaration
When the two declarations are two different things that were given one name by accident, the repair is a second name. Here the two values were never one total - one is the total, the other a subtotal on its way into it - so the second declaration takes its own name:
package main
import "fmt"
var total = 1
var subtotal = 2
func main() {
fmt.Println("total:", total)
fmt.Println("subtotal:", subtotal)
}
$ go build -trimpath -o p4.bin p4_fix_rename.go
$ ./p4.bin
total: 1
subtotal: 2
Both values survive, each under the name that says what it is, and every use of the old shared name elsewhere in the package has to be read once to confirm it meant the declaration it now reaches. Choose this fix when deleting either declaration would lose a value the program needs - two counters, two configuration values, two helpers with different bodies. The new name is not decoration: it is the declaration of intent that there really are two things.
Second fix: merge the duplicate into one declaration
When the second declaration is the first one repeated - a paste that landed twice, a leftover from a move, two fragments of what was always a single value - the repair is the opposite: keep one declaration and delete the other. If the two lines were two contributions to one value, the merged declaration says so in one place:
package main
import "fmt"
var total = 1 + 2
func main() {
fmt.Println("total:", total)
}
$ go build -trimpath -o p5.bin p5_fix_merge.go
$ ./p5.bin
total: 3
One declaration, one value, and the build is clean. In the plainer form of this fix the merge is just a deletion: the duplicate line goes, the surviving declaration keeps its original value, and nothing else in the file changes. Choose this fix when the two declarations agree about what the name is - same purpose, same intended value or two halves of it - and choose the rename when they do not. Reading both initializers, and both bodies in the cross-file shape, is what decides; the compiler's pair of positions puts both in front of you.
Neighboring error: a repeated short declaration
One error shares the word and is not this one. Inside a function, declaring the same variable twice with the short declaration operator draws a different message:
package main
import "fmt"
func main() {
x := 1
x := 2
fmt.Println(x)
}
$ go build -trimpath -o p6.bin p6_neighbour_no_new.go
# command-line-arguments
./p6_neighbour_no_new.go:7:4: no new variables on left side of :=
The specification gives short variable declarations a limited redeclaration exception of their own: a short declaration may redeclare variables provided they were originally declared earlier in the same block with the same type, and at least one of the non-blank variables on the left is new - which is why a lone repeated x := 2 fails: it introduces nothing new, so there is nothing for the exception to attach to, and the message says exactly that instead of calling the name redeclared. The repair there is an assignment, x = 2, or a multi-variable declaration with at least one genuinely new name. That error has its own guide, linked below; the boundary to hold here is that redeclared in this block is about two full declarations in one block, while no new variables is about a short declaration that declares nothing.
Decision summary
- The error names a variable, constant, or type, and the two positions are two full declarations in one file: one block holds the name twice - rename one declaration if they are two different things, or merge them into one if they are one thing repeated.
- The error names a function, and the two positions sit in two different files: the package block spans the files - compare both bodies, then delete the leftover or rename one if they are genuinely different functions.
- The second declaration sits inside braces, a function body, or a loop body, and the program builds: that is shadowing, not this error - the inner name is a separate variable, and only its readability is in question.
- The message instead reads
no new variables on left side of :=: a short declaration inside a function introduced no new name - use an assignment, or add a genuinely new variable to the declaration; it is a different error with its own guide. - After either fix, exactly one declaration of the name remains in the block, and the program builds and prints the value the surviving declaration was written to produce.
Checklist
- Both positions in the error were read: the rejected declaration on the first line, the standing "other declaration" on the indented line under it.
- The two declarations were compared before either was touched - values and bodies alike, in the cross-file shape as much as the one-file shape.
- The repair matches the intent: a rename when the two declarations are two different things, a merge or deletion when they are one thing repeated.
- No inner-block reuse was "fixed" as if it were a duplicate: a name redeclared in an inner block is shadowing, and it compiles on purpose.
- A repeated short declaration inside a function was not treated as this error: it draws the
no new variablesmessage and is repaired with an assignment or a new name in the declaration. - The repaired program builds, and running it prints the value the surviving declaration was meant to hold.
Related guides
- Fix Go "No New Variables on Left Side of :=" — Redeclaration, Scope, and Shadowing | Veldica - the neighboring error from the last section: a short declaration that introduces no new variable, its limited redeclaration exception, and when an assignment is the repair instead.
- Fix Go "Declared and Not Used" and "Imported and Not Used" Compile Errors | Veldica - the other declaration-housekeeping errors: a name declared once but never used is rejected just as firmly as a name declared twice.
- Fix Go "Undefined: ..." Compile Errors — Identifiers, Scope, and Package Qualifiers | Veldica - the opposite scope failure: a name used where no declaration of it is in scope at all, where here the block holds one declaration too many.
Sources
- The Go Programming Language Specification - the declarations and scope section states the rule this error enforces: no identifier may be declared twice in the same block, or in both the file and package block; the scope of a top-level identifier is the package block; and an identifier declared in a block may be redeclared in an inner block, denoting the inner entity while that declaration is in scope. Its short variable declarations section states the neighboring exception: a short declaration may redeclare variables originally declared earlier in the same block with the same type, provided at least one non-blank variable on the left is new.
Keep Exploring
The full Go hub \u2014 slices and maps, concurrency, and the standard library.
Every guide on the site in one list, grouped by topic.
Every browser-only checker and verifier on the site.
The C++, Python, Sysadmin, Rust, Go, and TypeScript hubs.