Why can I override the error in multiple return expressions in Go

Consider the following example illustrating the question (it was just built to explain the question, but I saw similar code in books and 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 does not complain about err overrides, 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 is the compiler happy to override err in a multi return statement using := but not n1 on a <- line 2 example?

It is better if you can provide an official link explaining this behavior.

+5
source share
1 answer

This is because you used a short variable declaration := . Quoting from the specification:

Unlike regular variable declarations, a short variable declaration can update variables if they were originally declared earlier in the same block (or a list of parameters if the block is a function body) of the same type, and at least one of the - blank variables are new . As a result, redeclaration can only appear in a short declaration with a few variables . Redeclaration does not introduce a new variable; he simply assigns a new meaning to the original.

This line:

 n1, err := strconv.Atoi("1") 

It is a brief declaration with several variables, and all the variables on the left are new, and so everyone will be declared (and strconv.Atoi() will be returned).

This line:

 n2, err := strconv.Atoi("2") 

This is a short declaration with several variables, and n2 is a new one. Thus, it declares n2 and assigns a new value to err , because err already declared in the same block.

This line:

 err := fmt.Errorf("new error") <- line 1 

This is not a short declaration with several variables. It will try to declare err , but it is already declared in the same block, so this is a compile-time error.

And this line:

 n1, err := strconv.Atoi("3") <- line 2 

This is a short declaration with several variables, but all the variables on the left side were previously declared in one block, so it is also a compiler (it does not introduce any new variables on the left side).

Please note that if all the variables on the left side were previously declared, simply changing the value from the declaration of the short variable := to Assignment = will make it work (the accepted values โ€‹โ€‹on the right side are assigned for the variables on the left).

+8
source

Source: https://habr.com/ru/post/1238116/


All Articles