当一个函数在Golang中返回多个变量时,变量的范围是什么?在附上的代码中,我无法弄清b的范围。Golang:函数返回值的范围
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)
}
该变量将持续到main的最后一行;然而,在第二次调用addMulti()时,'b'的值会发生变化。这是任何编程语言的典型特征,它与多次返回无关。 – weberc2 2015-01-21 16:19:21