英文:
Why redefining a variable does not always trigger an error?
问题
我很新于Go语言,已经遇到了几次以下问题。我不明白使用:=
来重新定义变量时的底层规则是什么(允许或不允许)。
请你解释一下为什么在第一个脚本中test := func3()
会触发错误,但在第二个脚本中却正常工作?
两个脚本之间唯一的区别是调用func3()
的行的位置。
谢谢!
第一个脚本
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)
}
英文:
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)
}
答案1
得分: 3
根据《Effective Go》中的描述,当左侧部分是一个名称列表时,:=
运算符会声明那些尚未声明的变量。只有当所有变量都已经声明时才会引发错误。
这在以下典型情况下特别方便:
a, err := someCall()
if (err) ...
b, err := someOtherCall()
if (err) ...
根据文档的术语:
在
:=
声明中,即使变量 v 已经被声明,它也可以出现,前提是:
- 此声明与变量 v 的现有声明在同一作用域中(如果 v 已经在外部作用域中声明,该声明将创建一个新变量),
- 初始化中的相应值可赋值给 v,并且
- 声明中至少有一个其他正在重新声明的变量。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论