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!
1st script
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)
}
2nd script
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)
}