Fix Go "No New Variables on Left Side of :=" — Redeclaration, Scope, and Shadowing
A short variable declaration does two jobs at once: it creates the variables on its left side and assigns the values on its right side. When every name on the left already exists in the same scope, there is nothing left to create, and the compiler refuses the statement with a single sentence: no new variables on left side of :=. The refusal is narrow. It says nothing about the values, the types, or the line above it — only that this :=, in this scope, would declare nothing. Each section below takes one shape of the refusal, or one shape that looks like the refusal and is not, and gives the fix that belongs to that shape.
Written November 18, 2026 · Tested with Go 1.26.4 · 25 min read
- The first error: the same name twice in one scope
- The plain fix: assignment, and its two limits
- The exact rule: redeclaration
- Mixed redeclaration: the form that passes
What this guide is
The := operator declares and assigns in one statement. That double duty has a condition attached: a declaration must declare something. When a := statement stands in a scope where every non-blank name on its left side is already declared, the statement would create no variable at all, and Go rejects it at compile time with the message no new variables on left side of :=. The fixes look similar from a distance — change the operator, add a name, move a brace — but each refusal shape has a different cause, and the wrong fix either fails again or compiles into a bug: a new variable that shadows the one the code meant to update. The examples here use Go 1.26.4. Every example lives in a single file named main.go inside one module, created once from an empty directory:
$ mkdir redeclare
$ cd redeclare
$ go mod init example.com/redeclare
go: creating new go.mod: module example.com/redeclare
Each program that follows replaces the contents of that main.go. When a build fails, the compiler prints the module path as a header line above the error — # example.com/redeclare here.
The first error: the same name twice in one scope
The smallest program that earns the message declares one variable and then declares it again, in the same scope, with nothing new beside it:
// main.go
package main
func main() {
x := 1
x := 2
_ = x
}
$ go build
# example.com/redeclare
./main.go:6:4: no new variables on left side of :=
The position, ./main.go:6:4, points at the := token itself: line 6 is the second declaration, and column 4 is where the operator begins. The message names no variable, because the defect is not in any one name — it is in the statement as a whole. Both names on the left side, all one of them, already exist in this scope, so the declaration half of := has no work to do. The _ = x line under the declarations changes nothing about this error; it keeps the unused-variable error out of the way so this message stands alone.
Note what the error is not. It is not a complaint that x changed, or that the second value has the wrong type, or that the first declaration was a mistake. The same program with the second statement written as an assignment is legal, and that contrast is the whole diagnosis: := failed because it is a declaration, and this scope needed an assignment.
The plain fix: assignment, and its two limits
When the intent is to give an existing variable a new value, the fix is to drop the colon. = assigns; it declares nothing, so it asks for nothing new:
// main.go
package main
import "fmt"
func main() {
x := 1
x = 2
fmt.Println(x)
}
$ go run main.go
2
The program prints 2: the second statement wrote into the same x the first statement created. This is the right fix whenever the variable already exists in the same scope and the new value fits its type. Assignment has two limits, and both print different errors from the one this guide is about, so they are worth separating at the start.
The first limit is that assignment cannot create. An = statement whose left side names nothing yet is not a declaration in disguise:
// main.go
package main
func main() {
x = 1
}
$ go build
# example.com/redeclare
./main.go:5:2: undefined: x
The error is undefined: x, at the name itself. There is no variable to assign to, and = will not make one. Code that meets this message needs the declaration it skipped: the first statement for a variable in a function is := or a var declaration, and only the statements after it may use =.
The second limit is that assignment cannot change a variable's type. A variable keeps the type its declaration gave it, and a value of another type offered by assignment is refused with the type error, not with this guide's message:
// main.go
package main
func main() {
x := 1
x = "two"
_ = x
}
$ go build
# example.com/redeclare
./main.go:6:6: cannot use "two" (untyped string constant) as int value in assignment
Here x exists, so there is no undefined complaint, and the statement is an assignment, so there is no no new variables complaint either. What remains is the type mismatch: "two" is a string, x is an int, and assignment performs no conversion between them. When the goal really is a value of a new type, the code needs a new variable to hold it — declared with := under a new name, which is a genuine declaration and passes — or an explicit conversion into the existing variable's type.
The exact rule: redeclaration
The specification states the exception that makes := useful in longer functions. A short variable declaration may redeclare variables, provided all of the following hold:
- The redeclared variable was originally declared earlier in the same block — or in the parameter list, when the block is the function body — with the same type.
- At least one of the non-blank variables on the left side is new.
- As a consequence of the first two conditions, redeclaration can only appear in a multi-variable short declaration.
Redeclaration does not introduce a new variable; it assigns a new value to the original. The non-blank variable names on the left side of := must also be unique, a separate condition with its own message, covered below. And short variable declarations may appear only inside functions — the package-level case is covered below as well, because it fails in a different way from everything else in this guide.
Every refusal in this guide is one of these conditions failing, and every passing mixed declaration is all of them holding at once. The scope condition is the subtle one: "the same block" is a precise term, and the sections on scope below show the same spelling passing on one side of a brace and meaning something else entirely on the other side.
Mixed redeclaration: the form that passes
The condition that does the work is the second one: at least one new non-blank name. A declaration that pairs an existing variable with a new one is legal, and the existing variable is assigned, not recreated:
// main.go
package main
import "fmt"
func main() {
x := 1
x, y := 2, 3
fmt.Println(x, y)
}
$ go run main.go
2 3
The output is 2 3. The second statement did two different things in one line: it declared y, which is new, and it assigned 2 to the x that already existed. Neither half alone would have the same shape — x := 2 on its own is the first error in this guide, and y := 3 on its own would leave x at 1.
The earlier declaration that a redeclaration builds on does not have to be a := statement. A variable declared with var in the same block can be redeclared in exactly the same way:
// main.go
package main
import "fmt"
func main() {
var x = 1
x, y := 2, 3
fmt.Println(x, y)
}
$ go run main.go
2 3
The output is again 2 3: x was declared by the var statement, assigned by the mixed declaration, and joined there by the new y. What matters is where and how the variable was declared, not which spelling declared it.
A function parameter counts as declared in the function body block, so a parameter can be redeclared by a mixed declaration in the body:
// main.go
package main
import "fmt"
func show(x int) {
x, y := 2, 3
fmt.Println(x, y)
}
func main() {
show(1)
}
$ go run main.go
2 3
The call passes 1, and the body prints 2 3: the mixed statement assigned 2 to the parameter x and declared y. No new x was created — inside the body there is only the one parameter, holding a new value.
The same type condition
A redeclared variable keeps its original type, and the value offered for it in the mixed declaration must be assignable to that type. Offering a string for an existing int variable fails inside an otherwise legal mixed declaration:
// main.go
package main
func main() {
x := 1
x, y := "two", 2
_, _ = x, y
}
$ go build
# example.com/redeclare
./main.go:6:10: cannot use "two" (untyped string constant) as int value in assignment
The message is the assignment form of the type error — in assignment, at the value "two" — because that is what redeclaration is for the existing name: an assignment. The new variable y on the same line is not the problem, and adding more new names does not repair the line. The repairs are the ones the type error always takes: offer a value of the variable's type, convert the value explicitly, or give the new-typed value a name of its own in a declaration of its own.
The blank identifier never counts as new
The rule says "non-blank" because the blank identifier _ is not a variable. It receives a value and discards it, it is never declared, and it never satisfies the at-least-one-new condition. Pairing an existing variable with a blank is therefore the same, to the rule, as declaring the existing variable alone:
// main.go
package main
func main() {
x := 1
x, _ := 2, 3
_ = x
}
$ go build
# example.com/redeclare
./main.go:6:7: no new variables on left side of :=
The error is the base message, reported at the := token — column 7 on this line, past the x, _ list. The _ on the left side is legal in itself; it simply does not count, so the statement has no new variable and fails as if the blank were not there.
The blank is still the right tool for the value a program does not need, in a declaration where some other name is genuinely new. Both positions work the same way — the blank discards its value and the named variable beside it carries the declaration:
// main.go
package main
import "fmt"
func main() {
x, _ := 1, 2
_, y := 3, 4
fmt.Println(x, y)
}
$ go run main.go
1 4
The output is 1 4: x took the first value of its pair and y took the second value of its pair, and the two discarded values went nowhere. The practical reading is that a line of the form x, _ := ... is a declaration only when x itself is new in this scope. When x already exists here, the line needs a real new name beside it, or it needs to be an assignment with the blank on its left side instead: x, _ = ....
A repeated name on the left side
Uniqueness is a separate condition, and it has a separate message. A name written twice in one declaration's left side is refused before the new-variable question arises:
// main.go
package main
func main() {
x, y, x := 1, 2, 3
_, _ = x, y
}
$ go build
# example.com/redeclare
./main.go:5:8: x repeated on left side of :=
The message names the repeated identifier and points at its second occurrence, column 8. This is not the no-new-variables error and does not take its fixes: the statement is malformed as a declaration list, whatever scope it stands in. Remove the duplicate name — and if the intent was to assign two of the values to the same variable, that is two statements, because one statement assigns each left-side name once, in order.
Where scope changes the answer: outer names are not redeclared
The same-block condition cuts both ways. A := statement whose names exist only in an outer scope does not redeclare them and does not fail. It declares new variables in the inner scope, and the outer ones are shadowed — hidden behind the new ones for the rest of the inner scope. This is the point where the error message stops and a quieter defect becomes possible, so each level deserves its own program.
A package-level variable is in the package block, not in any function body block, so a := inside a function never redeclares it:
// main.go
package main
import "fmt"
var count = 1
func show() {
fmt.Println(count)
}
func main() {
count, extra := 2, 3
fmt.Println(count, extra)
show()
}
$ go run main.go
2 3
1
The output is 2 3 and then 1. Inside main, the mixed declaration created a new count holding 2 and a new extra holding 3 — both names were new in that block, so no condition was strained. The package-level count was never touched, and the show function, which reads the package-level variable because it has no local one, still prints 1. A reader who expected the second line to update the shared count gets no error and a wrong program. The fix, when the shared variable is the intent, is assignment for the existing name and a declaration for the new one: count = 2 on one line and extra := 3 on the next.
An inner block is the same boundary in miniature. A := inside a braced block creates its variables in that block, even when every name it uses exists outside:
// main.go
package main
import "fmt"
func main() {
x := 1
{
x, y := 2, 3
fmt.Println(x, y)
}
fmt.Println(x)
}
$ go run main.go
2 3
1
The output is 2 3 and then 1. The inner declaration is legal — x and y are both new in the inner block — and it leaves the outer x at 1, as the last line shows. This answers the question the base error raises in reverse: a := with only outer names on its left side is not a no-new-variables case at all. It is a shadowing case, and the compiler accepts it without comment.
The scope of a short-declared variable begins at the end of its declaration statement, which has one consequence that reads like a trick. On the right side of an inner :=, the same name still denotes the outer variable:
// main.go
package main
import "fmt"
func main() {
x := 1
{
x := x + 1
fmt.Println(x)
}
fmt.Println(x)
}
$ go run main.go
2
1
The output is 2 and then 1. The inner statement read the outer x, added 1, and stored the result in a new inner x that ended at the closing brace. The outer variable was an input to the computation and was never assigned by it.
The shadowing trap
// main.go
package main
import "fmt"
func main() {
x := 10
{
x := 99
x = 100
fmt.Println(x)
}
fmt.Println(x)
}
$ go run main.go
100
10
The output is 100 and then 10. Inside the block, the declaration created a second x, the assignment raised that one to 100, and the print inside the block reports the inner value. At the closing brace the inner variable ends, and the outer x still holds 10 — the value it had before the block, untouched by everything the block did. No message marks any of this, because no rule was broken: declaring in an inner block and assigning in an inner block are both ordinary operations. The defect is only visible against the intent. When the block is meant to update the outer variable, it must contain no := for that name: open the block, assign with =, and the same statements write the outer variable. When the block is meant to work on its own value, the shadow is the right tool, and giving the inner variable a different name makes the separation plain to the next reader.
Initializers: if, for, and switch
A short declaration may also stand as the initializer of an if, for, or switch statement. The variable it declares is scoped to that statement — the specification puts it among the local temporary variables a statement may declare — and within the statement it shadows any outer variable of the same name:
// main.go
package main
import "fmt"
func main() {
x := 1
if x := 2; x > 0 {
fmt.Println(x)
}
fmt.Println(x)
}
$ go run main.go
2
1
The output is 2 and then 1. The initializer declared a new x for the if statement, the body printed that one, and the outer x kept its value throughout. The same spelling inside the statement and outside it names two variables, and neither statement about one says anything about the other.
The variable a for initializer declares lives for the loop and ends with it, which the counting in this program uses without ever naming the variable after the loop:
// main.go
package main
import "fmt"
func main() {
total := 0
for i := 0; i < 3; i++ {
total += i
}
fmt.Println(total)
}
$ go run main.go
3
The output is 3, the sum of 0, 1, and 2. Referring to the loop variable after the statement is not a shadowing subtlety but a plain scope error — the name does not exist there:
// main.go
package main
import "fmt"
func main() {
for i := 0; i < 1; i++ {
}
fmt.Println(i)
}
$ go build
# example.com/redeclare
./main.go:9:14: undefined: i
The message is undefined: i, at the use. A value that must outlive the loop has to live in a variable declared before the loop, with the loop's statements assigning to it — as total does in the program above, declared before the for and updated inside it.
A switch initializer follows the same pattern, with the declared variable visible in the case clauses:
// main.go
package main
import "fmt"
func main() {
switch n := 2; n {
case 2:
fmt.Println("two", n)
}
}
$ go run main.go
two 2
The output is two 2: the initializer declared n, the switch matched on it, and the matching case read it. The variable ends with the switch statement, as the if and for variables end with theirs.
One consequence closes the section. Because the variable an initializer declares ends with its statement, a := for the same name after the statement is a fresh declaration, not a redeclaration and not an error:
// main.go
package main
import "fmt"
func main() {
if x := 1; x > 0 {
fmt.Println(x)
}
x := 2
fmt.Println(x)
}
$ go run main.go
1
2
The output is 1 and then 2. The first x belonged to the if statement and ended with it; the second statement declares the x that the last line prints. Code that wants the second statement to continue working with the value the initializer declared cannot — that variable is gone — and needs the value declared before the if instead, with the statement testing it rather than declaring it.
Package level: := is not a declaration there
Short variable declarations may appear only inside functions. At package level the := form is not a weaker declaration or a redeclaration case; it is not a declaration at all, and the failure is a syntax error:
// main.go
package main
x := 1
func main() {}
$ go build
# example.com/redeclare
./main.go:4:1: syntax error: non-declaration statement outside function body
The message is syntax error: non-declaration statement outside function body, at the statement itself. Package-level variables are declared with var, with or without an initializer — var x = 1 and var x int = 1 are both ordinary package-level declarations — and a function that needs to change such a variable assigns to it with =, as the scope section above shows. Moving the line inside a function is not the fix for a package-level intent; it changes which variable the program has.
Parallel forms: swaps and the error pattern
Two everyday shapes put several names on the left side at once, and both meet the rule head-on. The first is the swap. A pair of variables trade values in one assignment, because the right side is evaluated fully before any assignment takes place:
// main.go
package main
import "fmt"
func main() {
a, b := 1, 2
a, b = b, a
fmt.Println(a, b)
}
$ go run main.go
2 1
The output is 2 1. Note the operator on the second line: =, not :=. The swap assigns to two existing variables and declares nothing, so it is an assignment by definition, and writing it with := reproduces the base error in its multi-variable form:
// main.go
package main
func main() {
a, b := 1, 2
a, b := b, a
_, _ = a, b
}
$ go build
# example.com/redeclare
./main.go:6:7: no new variables on left side of :=
Same message, at the := token. Two existing names are no more new in a pair than one is alone; the at-least-one-new condition counts the whole left side, and a left side with nothing new fails at any width.
The second shape is the multi-value call, and it is where redeclaration earns its place. A function that returns a value and an error is called repeatedly in one function, and each call needs a new name for its value while the error variable is shared:
// main.go
package main
import (
"fmt"
"strconv"
)
func main() {
v, err := strconv.Atoi("42")
fmt.Println(v, err)
v2, err := strconv.Atoi("7")
fmt.Println(v2, err)
}
$ go run main.go
42 <nil>
7 <nil>
The output is 42 <nil> and then 7 <nil>. The first call declares both v and err. The second call declares v2 — the new name that satisfies the rule — and redeclares err, which is assigned the second call's error in place. This is the pattern Effective Go uses to explain redeclaration: a single err value carried through a sequence of calls, each statement adding one new variable beside it. It works because the two calls stand in the same scope. Move the second call inside a block, and the same spelling would declare a new err scoped to the block instead, leaving the outer err holding the first call's result — the shadowing section's trap, in the shape working code meets most often.
The fixes, ranked
Assign with = when the variable exists in this scope and the value fits its type. This is the fix for the base error and for the repeated swap: the statement was meant to write, not to declare, and the colon was the only defect.
Add a genuinely new name when the statement brings in a new value. The mixed form — one new variable beside the redeclared ones — is the fix for the repeated-call pattern: a new result name on each call, the shared err redeclared beside it. The new name must be a real variable; a blank in that position does not count.
Split the statement when the intents differ. An update to a package-level or outer variable and the creation of a local one are two operations. Written as two statements — assignment for the existing name, declaration for the new one — each is checked against its own rule and neither shadows anything by accident.
Remove the := from inside a block when the block must write the outer variable. A block that updates is a block with assignments in it. If the block declares the name instead, every write in the block lands on the inner variable and the outer value survives unchanged, with no message at any point.
Declare before the statement when a value must outlive an if, for, or switch. Initializer variables end with their statement. A result the rest of the function needs belongs in a variable declared before the statement and assigned inside it.
Use var at package level. There is no short form there. A package-level name is a var declaration, and functions change it by assignment.
Which fix for which cause
| What the code shows | Cause | Fix |
|---|---|---|
One existing name declared again in the same scope (x := 1 then x := 2) |
The statement declares nothing new; := is a declaration, not an assignment |
Change the second statement to an assignment: x = 2 |
A swap or update written with := (a, b := b, a after a, b := 1, 2) |
Both names already exist in the scope; the left side as a whole has nothing new | Write the update with =: a, b = b, a |
An assignment to a name that does not exist yet (x = 1 as a first statement) |
Assignment cannot create a variable | Declare first with := or var; assign afterwards |
A value of a new type offered to an existing variable, by = or by mixed := |
A variable keeps its declared type; neither assignment nor redeclaration converts | Convert explicitly, or hold the new-typed value in a new variable |
A repeated call where only the error name repeats (v, err := ... then a wish to reuse both spellings) |
Redeclaration needs at least one new non-blank name on the left side | Give each call a new result name (v2, err := ...); err is redeclared, the result name carries the declaration |
An existing name paired only with a blank (x, _ := ... with x already declared here) |
The blank identifier is never declared and never counts as new | Add a real new name, or make the statement an assignment: x, _ = ... |
The same name twice in one left side (x, y, x := 1, 2, 3) |
Non-blank names on the left side of := must be unique |
Remove the duplicate; use two statements if both values must reach one variable |
A := inside a block using only outer names, with the outer value unchanged afterwards |
The inner declaration is legal and creates a new variable; the outer one is shadowed, not updated | Assign with = inside the block to write the outer variable, or rename the inner variable if the shadow is intended |
A := in a function using a package-level name, with the shared value unchanged |
The function block is a different scope from the package block; the statement declared a local variable | Assign to the package-level name with =, and declare any new local separately |
A name from an if, for, or switch initializer used after the statement (undefined: i) |
An initializer variable is scoped to its statement and ends with it | Declare the variable before the statement and assign inside it |
A := at package level |
Short declarations appear only inside functions; at package level the form is not a declaration | Declare with var; change the value from functions with = |
Checklist
- Before changing the operator, name the scope the statement stands in and the scope each left-side name was declared in. Same scope is a redeclaration question; different scopes are a shadowing fact, and the two take different fixes.
- Before adding a name to satisfy the rule, check that the name is a real variable. The blank identifier does not count, in either position.
- Before redeclaring, check the type: a redeclared variable is assigned, and the value must be assignable to the type it already has.
- Before writing a
:=inside a block, decide which variable the block's writes are for. If they are for the outer variable, the block holds assignments, not a declaration of that name. - Before relying on an initializer variable, check where it ends: at the end of its if, for, or switch statement. Anything needed later is declared before the statement.
- Before reaching for
:=at package level, stop: the form does not exist there, and the error it earns is a syntax error, not this guide's message.
Related guides on this site
- Fix Go "Declared and Not Used" and "Imported and Not Used" Compile Errors — the companion refusal from the same first hour with the language, where the compiler rejects a declaration for being unused rather than for declaring nothing new, and where the blank identifier is the deliberate-discard mechanism this guide relies on.
- Fix Go "Assignment Mismatch" and "Multiple-Value in Single-Value Context" Errors — the counting errors on the same statements: the declarations there fail because the two sides hold different numbers of values, the declarations here because the left side holds no new name.
- Fix Go "Cannot Use ... As ..." Type Errors — Assignability, Conversions, and Untyped Constants — the type refusal that a mixed redeclaration prints when the value offered for an existing variable does not fit its type, stated there as the assignment form of the message.
- Go Structs and Interfaces — the named types and method sets that multi-value calls return, in the pattern where one new result name and one redeclared error name carry a sequence of calls.
Sources
- The Go Programming Language Specification — for short variable declarations (the syntax
IdentifierList := ExpressionList; shorthand for a regular declaration with initializers and no types; redeclaration of variables originally declared earlier in the same block, or the parameter lists if the block is the function body, with the same type, when at least one of the non-blank variables is new; redeclaration only in a multi-variable short declaration; redeclaration assigns a new value to the original rather than introducing a new variable; the non-blank names on the left side must be unique; short declarations may appear only inside functions and may declare local temporary variables in if, for, and switch initializers), for scope (Go is lexically scoped using blocks; the scope of a variable declared inside a function begins at the end of its declaration — the ShortVarDecl for a short declaration — and ends at the end of the innermost containing block; an identifier declared in a block may be redeclared in an inner block, and while the inner declaration is in scope it denotes the entity the inner declaration declared), and for declarations (a short variable declaration is not among the declarations that may appear at package level, where a non-declaration statement is a syntax error). - Effective Go — for redeclaration and reassignment: in a
:=declaration a variable may appear even if it has already been declared, provided the declaration is in the same scope as the existing declaration — if the variable is already declared in an outer scope, the declaration creates a new variable — the corresponding value is assignable to the variable, and at least one other variable is created by the declaration; and for the worked pattern behind it, in whichf, err := os.Open(name)declaresfanderr, and a later short declaration for the file's Stat result, in the same scope, reassigns the existingerrwhile declaringd.
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.