The cause is detailed in Spec: Short variable declaration:
Unlike regular variable declarations, a short variable declaration may redeclare variables provided they were originally declared earlier in the same block (or the parameter lists if the block is the function body) with the same type, and at least one of the non-blank variables is new. As a consequence, redeclaration can only appear in a multi-variable short declaration. Redeclaration does not introduce a new variable; it just assigns a new value to the original.
When using short variable declaration with multiple variables where one already exists, assignment will only happen to the existing variable if it was declared in the same block. Since in your case err
variable existed before the if
block, a new err
variable will be created inside the if
block, which has nothing to do with the "outsider" err
variable (other than sharing its name). The outer err
will be shadowed in the if
block after the short variable declaration.
So what happens is that inside the if
, you create a new err
variable, and you assign a value to that, and you print that.
After the if
statement, you will print the outer err
variable whose value was not changed inside the if
block, so it remains nil
.
See this example:
var err error
fmt.Println("outside:", err) // nil
{
// A new err variable, shadows the outer:
i, err := strconv.Atoi("a")
fmt.Println("inside:", i, err) // 0 strconv.Aoti: parsing "a": invalid syntax
}
fmt.Println("outside:", err) // nil, because this was never changed
// Now this will change the "outer" err:
j, err := strconv.Atoi("a")
fmt.Println("outside:", j, err) // 0 strconv.Aoti: parsing "a": invalid syntax
Output (try it on the Go Playground):
outside: <nil>
inside: 0 strconv.Atoi: parsing "a": invalid syntax
outside: <nil>
outside: 0 strconv.Atoi: parsing "a": invalid syntax
If you want to use (assign to) the "outer" variable when also creating new variable(s), you can't use short variable declaration in a "nested" block but only simple assignment, in which case you also have to declare the other variables a priori, like in this example:
if err == nil {
var body []byte
body, err = ioutil.ReadAll(response.Body)
// ... rest...
}
See related questions:
Why it is possible to redefine err in multiple return statement in Go
Why there are two ways of declaring variables in Go, what's the difference and which to use?