英文:
How to differentiate between assigning and declaring values from a function with multiple returns?
问题
当从函数中检索多个返回值时,你可以使用:=
在使用中声明变量来获取返回值,或者使用=
将返回值赋给已经存在的变量。但是,当我想将一个返回值赋给已经存在的变量,并为另一个返回值声明一个新变量时,我遇到了问题。
目前,我通过先声明所需的变量(在这种情况下是bar
),然后只赋值来解决了这个问题,就像这个代码片段中所示:
package main
import (
"fmt"
)
func getFooAndBar() (foo string, bar string) {
return "Foo", "Bar"
}
func main() {
var foo = "default"
var condition = true
if condition {
var bar string // 如果可能的话,我想避免这一步
foo, bar = getFooAndBar()
fmt.Println(bar)
}
fmt.Println(foo)
}
如果我使用:=
,它无法构建,因为会出现以下错误:
./app.go:16: foo declared and not used
所以,有没有办法避免单独声明bar
这一步呢?
英文:
When retrieving multiple returns from a function, I get that you can declare variables for the values on the fly by using :=
or assign the values to already existing variables by simply using =
. My issue occurs when I want to assign one of the return values to an already existing variable while declaring a new variable for the other.
I have currently solved it by only assigning the values and declaring the required variables (bar
in this case) beforehand, as in this snippet:
package main
import (
"fmt"
)
func getFooAndBar() (foo string, bar string) {
return "Foo", "Bar"
}
func main() {
var foo = "default"
var condition = true
if condition {
var bar string // Would like to avoid this step if possible
foo, bar = getFooAndBar()
fmt.Println(bar)
}
fmt.Println(foo)
}
If I use :=
it fails to build due to:
>./app.go:16: foo declared and not used
So, is it possible to somehow avoid the step declaring bar
separately?
答案1
得分: 5
在这种情况下,根据规范,你不能使用短变量声明:=
来重新声明foo
变量:
与常规变量声明不同,短变量声明可以重新声明变量,前提是它们在同一块中以相同的类型最初被声明,并且至少有一个非空白变量是新的。因此,重新声明只能出现在多变量的短声明中。重新声明不会引入新变量;它只是给原始变量赋予新值。
通过消除./app.go:16: foo declared and not used
错误。
func main() {
var foo = "default"
var condition = true
if condition {
foo, bar := getFooAndBar()
fmt.Println(bar) // 输出:Bar
fmt.Println(foo) // 输出:Foo
// _ = foo
}
fmt.Println(foo) // 输出:default
}
在这种情况下,foo
在if
块中被声明,这个声明将创建一个新变量,遮蔽了外部块中的原始foo
变量,只有在同一块中使用多变量短声明重新声明foo
时,才会发生重新声明。
func main() {
var foo = "default"
foo, bar := getFooAndBar()
fmt.Println(bar) // 输出:Bar
fmt.Println(foo) // 输出:Foo
}
英文:
In this case you can't use the short variable declarations ":="
for redeclaring the foo
variable, according to the spec:
> Unlike regular variable declarations, a short variable declaration may
> redeclare variables provided they were originally declared earlier in
> the same block with the same type, and at least one of the non-blank
> variables is new. As a consequence, redeclaration can only appear in a
> multi-variable short declaration. Redeclaration does not introduce a
> new variable; it just assigns a new value to the original.
by eliminating ./app.go:16: foo declared and not used
.
func main() {
var foo = "default"
var condition = true
if condition {
foo, bar := getFooAndBar()
fmt.Println(bar) // prints: Bar
fmt.Println(foo) // prints: Foo
// _ = foo
}
fmt.Println(foo) // prints: default
}
in this case foo
is declared in the if
block, this declaration will create a new variable shadowing the original foo
variable in the outer block, the redeclaration of foo
will happen only if you have declared foo
and redeclared it with multi-variable short declaration within the same block.
func main() {
var foo = "default"
foo, bar := getFooAndBar()
fmt.Println(bar) //prints: Bar
fmt.Println(foo) //prints: Foo
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论