package main
import "fmt"
var a = 4
func main() {
fmt.Println(a, add(), a) //Output:8 8 8
}
func add() int {
a += 4
return a
}
[goplay][1]
**示例的结果与spec中描述不符**
[**Order of evaluation**][2]
When evaluating the operands of an expression, assignment, or return statement, all function calls, method calls, and communication operations are evaluated in lexical **left-to-right order.**
For example, in the assignment
y[f()], ok = g(h(), i()+x[j()], <-c), k()
the function calls and communication happen in the order f(), h(), i(), j(), <-c, g(), and k(). However, the order of those events compared to the evaluation and indexing of x and the evaluation of y is not specified.
Floating-point operations within a single expression are evaluated according to the associativity of the operators. Explicit parentheses affect the evaluation by overriding the default associativity. In the expression x + (y + z) the addition y + z is performed before adding x.
[1]: http://play.golang.org/p/kfzVt0YoSl
[2]: http://golang.org/ref/spec#Order_of_evaluation