當一個函數在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