Golang:函数的多返回值的作用域

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

Golang: Scope of multi return values from function

问题

当一个函数在Golang中返回多个变量时,这些变量的作用域是什么?在附上的代码中,我无法确定变量b的作用域。

package main

import (
	"fmt"
)

func addMulti(x, y int) (int, int) {
	return (x + y), (x * y)
}

func main() {
	//这里的b变量的作用域是什么?
	a, b := addMulti(1, 2)

	fmt.Printf("%d %d\n", a, b)

	//这里的b变量的作用域是什么?
	c, b := addMulti(3, 4)

	fmt.Printf("%d %d\n", c, b)

}

在Golang中,变量的作用域由其声明的位置决定。在这个例子中,变量b的作用域仅限于每个代码块(由大括号{}包围的代码段)内部。因此,在第一个代码块中声明的变量b只在该代码块内部可见,在第二个代码块中声明的变量b也只在该代码块内部可见。这意味着两个代码块中的变量b是不同的变量,它们之间没有任何关联。

希望这可以帮助到你!如果你有任何其他问题,请随时问。

英文:

When a function returns more than one variable in Golang, what's the scope of the variables? In the code attached, I can't figure out the scope of b.

package main

import (
	"fmt"
)

func addMulti(x, y int) (int, int) {
	return (x + y), (x * y)
}

func main() {
	//what is the scope of the b variable here?
	a, b := addMulti(1, 2)

	fmt.Printf("%d %d\n", a, b)

	//what is the scope of the b variable here?
	c, b := addMulti(3, 4)

	fmt.Printf("%d %d\n", c, b)

}   

答案1

得分: 10

我们不讨论函数返回值的作用域,而是讨论你将返回值赋给的变量的作用域。

在你的情况下,变量b的作用域是函数体,在你声明它的那一点。

首先,在这一行中你这样做:

a, b := addMulti(1, 2)

但是后来你在这一行使用了另一个短变量声明

c, b := addMulti(3, 4)

由于b已经声明过了,这只是给它赋了一个新值。b的作用域将一直持续到main()函数的结束。引用自Go语言规范:

> 与常规变量声明不同,短变量声明可以重新声明变量,前提是它们在同一块中以相同的类型进行了原始声明,并且至少有一个非空白变量是新的。因此,重新声明只能出现在多变量的短变量声明中。重新声明不会引入新变量;它只是给原始变量赋予一个新值。

英文:

We're not talking about the scope of the return value of a function but rather the scope of the variable you assign the return value to.

The scope of the variable b in your case is the function body, from the point at which you declare it.

At first you do it at this line:

a, b := addMulti(1, 2)

But then you use another Short Variable declaration at this line:

c, b := addMulti(3, 4)

which - since b is already declared - just assigns a new value to it. b will be in scope until the end of your main() function. Quoting from the Go Language Specification:

> 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.

答案2

得分: 0

这是一个块内的普通变量。根据规范

在函数内声明的常量或变量标识符的作用域从ConstSpec或VarSpec(对于短变量声明为ShortVarDecl)的结束开始,并在最内层包含块的结束处结束。

在第二次调用中,你只是重新赋值给了同一个 b 变量。它的作用域是相同的。

英文:

It's a normal variable inside a block. From the spec:

> The scope of a constant or variable identifier declared inside a function begins at the end of the ConstSpec or VarSpec (ShortVarDecl for short variable declarations) and ends at the end of the innermost containing block.

In the second call, you're just reassigning the value of the same b variable. Its scope is the same.

答案3

得分: 0

变量b的作用域是main.main()。在第二个赋值语句c, b := addMulti(3, 4)中,你引入了一个新变量c,并将第一个赋值语句中引入的变量b赋值给它。如果你将第二个赋值语句改为a, b := addMulti(3, 4),与第一个赋值语句相同,它将无法编译通过。

英文:

The scope of b variable is main.main(). In second assignment c, b := addMulti(3, 4) you introduce new variable c, and assign variable b introduced in first assignment. If you change second assignment to be a, b := addMulti(3, 4) same as first it want not to compile.

答案4

得分: -1

如果是这种情况,请尝试以下代码:

func makeRequest(uri string, payload string) {
    var resp http.Response
    var err error

    if payload == "" {
        resp, err := http.Get(uri)
    }
    if err != nil {
        return
    }

    bytes, err := io.ReadAll(resp.Body)
    fmt.Println(bytes)
}
英文:

If that's the case, try the following code:

func makeRequest(uri string, payload string) {
	var resp http.Response
	var err error

	if payload == "" {
		resp, err := http.Get(uri)
	}
	if err != nil {
		return
	}

	bytes, err := io.ReadAll(resp.Body)
	fmt.Println(bytes)
}

huangapple
  • 本文由 发表于 2015年1月21日 19:44:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/28066434.html
匿名

发表评论

匿名网友

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

确定