I'm quite new to Go and I've stumble upon the following problem a few times already. I don't understand what's the underlying rule that allows (or not) the redefining of one variable with the help of :=
.
Can you please explain to me why test := func3()
does trigger an error in the first script but works fine in the second script?
The only thing that changes between both scripts is the position of the line that calls func3()
.
Thanks!
https://play.golang.org/p/vvCI7nxHZL
package main
import (
"fmt"
)
func func1() (string, string) {
return "", ""
}
func func2() (string, string) {
return "", ""
}
func func3() string {
return ""
}
func main() {
test, test2 := func1()
test, test3 := func2()
test := func3()
fmt.Println(test, test2, test3)
}
https://play.golang.org/p/BTTYCGEJ4E
package main
import (
"fmt"
)
func func1() (string, string) {
return "", ""
}
func func2() (string, string) {
return "", ""
}
func func3() string {
return ""
}
func main() {
test := func3()
test, test2 := func1()
test, test3 := func2()
fmt.Println(test, test2, test3)
}
As described in Effective Go, the :=
operator, when the left part is a list of names, declares the ones which aren't already declared. An error is only raised when all are already declared.
This is especially convenient in this typical case:
a, err := someCall()
if (err) ...
b, err := someOtherCall()
if (err) ...
In the documentation terms:
In a := declaration a variable v may appear even if it has already been declared, provided:
- this declaration is in the same scope as the existing declaration of v (if v is already declared in an outer scope, the declaration will create a new variable §),
- the corresponding value in the initialization is assignable to v, and
- there is at least one other variable in the declaration that is being declared anew.