Consider the following example illustrating the question (it was just built to explain the question, but I saw similar code in books as well in real projects):
package main
import (
"strconv"
"fmt"
"log"
)
func main() {
n1, err := strconv.Atoi("1")
if err != nil {
log.Panicf("%v", err)
}
n2, err := strconv.Atoi("2")
if err != nil {
log.Panicf("%v", err)
}
// err := fmt.Errorf("new error") <- line 1
// n1, err := strconv.Atoi("3") <- line 2
fmt.Printf("n1 = %d, n2 = %d\n", n1, n2)
}
The compiler doesn't complain about redefining err, but if I uncomment <- line 1 or <- line 2, it will complain no new variable on left side of :=.
So, how does it work? Why the compiler happily allows to override err in multi return statement, using :=, but not n1 on <- line 2 example?
Better if you can point into the official reference explaining this behavior.