Go uses lexical (block) scoping. A name is visible from where it is declared to the end of the innermost block that contains it, and package-level names are visible across the whole package. Shadowing happens when an inner scope declares a name that already exists in an outer scope, so the inner name hides the outer one. It is legal and sometimes useful, but the short declaration := can create a shadowed variable by accident, which is a classic source of bugs.
Constants follow the same rules as variables, and the universe scope contains the predeclared identifiers such as len, string, true and nil.
| Scope | Where declared | Visible in |
|---|---|---|
| Universe | Predeclared by the language (int, len, nil, true) | Every Go file |
| Package | Outside any function | All files of the package; exported if capitalised |
| File | Import declarations | Only the file that imports |
| Block | Inside functions, if, for, switch, {} | From the declaration to the end of the block |
A package-level name starting with an upper-case letter is exported and visible to other packages. Lower-case names stay private to the package.
Declaring a variable with the same name in an inner block creates a new variable. The outer one keeps its value.
The inner x is a different variable. Assigning with = instead of := would modify the outer one.
With :=, at least one variable on the left must be new in the current scope. In a nested block, all of them are new, so an err declared inside an if or for hides the outer err.
Fix it by declaring the other variable first and using =:
Also watch the same pattern inside if x, err := f(); err != nil {}: both names live only within that if statement, including its else branches, which is intentional and usually good.
Go does not forbid shadowing imports or built-ins, but it is confusing.
Avoid names like len, cap, new, string, error, copy, max and min. max and min became built-ins in Go 1.21, so older code that declared its own max function still compiles because package-level declarations shadow the universe scope.
Closures capture variables, not values. Before Go 1.22, a for loop reused one variable for all iterations, which caused the well-known bug where every goroutine saw the final value. Since Go 1.22, each iteration has its own copy of the loop variable when the module's go directive is 1.22 or higher.
go vet; the shadow analyzer is available separately as golang.org/x/tools/go/analysis/passes/shadow/cmd/shadow and can be enabled in golangci-lint through govet.if v, err := f(); err != nil for values used only in the condition.= and a pre-declared variable when you intend to modify an outer variable.Yes, in an inner scope you can declare a new constant or variable with the same name. The outer constant is untouched. A constant itself can never be reassigned.
Go has package-level variables. They are visible package-wide and, if capitalised, to importers. Avoid mutable ones in concurrent code, or protect them with a mutex or use sync/atomic.
A variable declared in an inner scope, including a shadowing one, must be used there. This error often reveals an accidental :=.
Occasionally, for example x := x to copy a loop variable in code for pre-1.22 semantics, or err inside a small if statement. Otherwise, choose distinct names.
Go scopes are lexical: universe, package, file and block. An inner declaration shadows an outer one with the same name, and := in a nested block is the usual cause of accidental shadowing, especially with err. Keep scopes small, avoid shadowing built-ins and imports, use = to update outer variables, and run linters to catch mistakes.