---
title: No Variable Shadowing
impact: HIGH
impactDescription: prevents subtle bugs where inner variables hide outer ones
tags: variables, shadowing, scope, quality
---

## No Variable Shadowing

Variable shadowing occurs when a variable declared within a certain scope has the same name as a variable in an outer scope. This can lead to subtle and hard-to-debug logic errors.

**Incorrect (shadowed variables):**

```go
func processItems(items []string) {
    user := "admin"

    for _, item := range items {
        user := findUser(item) // Shadowing outer 'user' with := 
        fmt.Println("Processing for user:", user)
    }

    // Outer 'user' is still "admin", which might be unexpected if the loop 
    // was intended to update it.
}
```

**Correct (unique names or explicit assignment):**

```go
func processItems(items []string) {
    globalUser := "admin"

    for _, item := range items {
        currentUser := findUser(item)
        fmt.Println("Processing for user:", currentUser)
    }
}
```

**Tools:** `go vet -shadow`, GolangCI-Lint (shadow)
