如何区分在具有多个返回值的函数中分配和声明值?

huangapple go评论84阅读模式
英文:

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
}

在这种情况下,fooif块中被声明,这个声明将创建一个新变量,遮蔽了外部块中的原始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
}

huangapple
  • 本文由 发表于 2015年5月24日 06:40:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/30418259.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定