The Short Variable Declaration Shadow Gotcha in Go

The short variable declaration `:=` creates a new variable that shadows outer variables, leaving the original unchanged.

The short variable declaration (:=) creates a new variable in the innermost scope that shadows the outer variable with the same name, causing the outer variable to remain unchanged. This often happens when a loop or nested block re-declares a variable instead of reassigning it.

var x int
for i := 0; i < 10; i++ {
    // 'i' is a new variable local to the loop
    // It shadows any outer 'i' if it existed
}
// 'i' is undefined here if it was only declared with := inside the loop

To fix this, use the assignment operator (=) if the variable already exists in the outer scope, or ensure the variable is declared in the correct scope.