求值顺序

Hubery · 2013-12-03 02:14:28 · 4259 次点击 · 大约8小时之前 开始浏览    置顶
这是一个创建于 2013-12-03 02:14:28 的主题,其中的信息可能已经有所发展或是发生改变。

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

示例的结果与spec中描述不符

Order of evaluation

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.


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

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

4259 次点击  
加入收藏 微博
2 回复  |  直到 2015-06-19 02:39:14
Hubery
Hubery · #1 · 11年之前

参数传递时,压栈顺序和参数计算顺序不是一回事,而且是先计算再压栈。

peterrk
peterrk · #2 · 10年之前

fmt.Println接受的参数是interface,本质上是一种引用...

添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传