Just to clarify, those are not entirely equivalent. As I stated in my earlier post, when declaring and assigning in the if statement, variables are local to the if scope, not the surrounding function. So, in the first case it’s wrong to try to use the variables after the if (or if - else if - else) scope, i.e.:
func someFunc() {
if i, err := strconv.Atoi("42"); err == nil {
/*
Some statements should go here.
i and err may be used here.
*/
} else {
// Here i and err are fine too, this is still the if scope.
}
fmt.Println(err) //Wrong, err has not been declared in this scope, this won't compile.
}
On the second case, variables will be local to the function scope, so this is fine:
func someFunc() {
i, err := strconv.Atoi("42")
if err == nil { /* statements */ }
fmt.Println(err) //This is fine, err is well defined here.
}
This is explained at the Go tour, on the section I linked before.