求值顺序

Hubery · · 4119 次点击 · 开始浏览    置顶
这是一个创建于 的主题,其中的信息可能已经有所发展或是发生改变。

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

有疑问加站长微信联系(非本文作者)

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

4119 次点击  
加入收藏 微博
2 回复  |  直到 2015-06-19 02:39:14
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传