英文:
Execution order for GoLang Struct Initializer
问题
我现在是你的中文翻译助手,以下是你要翻译的内容:
想知道在Go语言中,结构体初始化的执行顺序是否是有保证的。
下面的代码是否总是会产生 obj.a == 1
和 obj.b == 2
,还是说它的行为是未指定的?
num := 0
nextNumber := func() int {
num += 1
return num
}
type TwoNumbers struct {
a int
b int
}
obj := TwoNumbers{
a: nextNumber(),
b: nextNumber(),
}
英文:
Was wondering if execution order for struct initialization is guaranteed in GoLang.
Does the following code always produce
obj.a == 1
and obj.b == 2
or is it unspecified behavior?
num := 0
nextNumber := func() int {
num += 1
return num
}
type TwoNumbers struct {
a int
b int
}
obj := TwoNumbers{
a: nextNumber(),
b: nextNumber(),
}
答案1
得分: 10
函数调用的评估顺序是指定的。
代码TwoNumbers{a: nextNumber(), b: nextNumber()}
是一个复合字面表达式。nextNumber()
的调用是表达式中的操作数。
规范关于表达式的一般规定如下:
> ...在评估表达式、赋值或返回语句的操作数时,所有函数调用、方法调用和通信操作都按照词法从左到右的顺序进行评估。
字段a
的函数调用在字段b
的函数调用之前进行评估,因为字段a
的函数调用位于字段b
的函数调用的左侧。
评估顺序仅对函数调用、方法调用和通信操作进行了指定。编译器可以重新排序其他操作数的评估。在下面的示例中,字段a
的表达式在词法上位于字段b
的表达式的左侧,但是b
先被评估(在当前编译器中)。
type Numbers struct{ a, b, c int }
nums := Numbers{a: num, b: nextNumber(), c: nextNumber()}
fmt.Println(nums) // 输出 {2 1 2}
英文:
The evaluation order of the function calls is specified.
The code TwoNumbers{a: nextNumber(), b: nextNumber()}
is a composite literal expression. The calls to nextNumber()
are operands in the expression.
The specification says this about expressions in general:
> ... 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.
The function call for field a
is evaluated before the function call for field b
because the function call for field a
is to the left of the function call for field b
.
The evaluation order is specified for function calls, method calls and communication operations only. The compiler can reorder the evaluation of other operands. In the following example, the expression for field a
lexically to the left of the expression for field b
, but b
is evaluated first (in the current compiler).
type Numbers struct{ a, b, c int }
nums := Numbers{a: num, b: nextNumber(), c: nextNumber()}
fmt.Println(nums) // prints {2 1 2}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论