-
Notifications
You must be signed in to change notification settings - Fork 18.5k
Description
From https://golang.org/ref/spec#Package_initialization:
Initialization proceeds by repeatedly initializing the next package-level variable that is earliest in declaration order and ready for initialization, until there are no variables ready for initialization.
[…]
For example, given the declarations
var ( a = c + b b = f() c = f() d = 3 ) func f() int { d++ return d }the initialization order is
d,b,c,a.
If Go initializes b before c, then after initialization I'd expect the value of b to be 4 and c to be 5. However, this test outputs b as 5 and c as 4. Swapping the b and c declarations doesn't change the output, but swapping the order in the addition in the declaration of a does change the output. Does this mean that the initialization order in the example is actually d, c, b, a? And that both LHS and RHS are in scope in the phrase "earliest in declaration order"? Or (more likely) am I missing something about what it means to declare and initialize a variable?
P.S. Location in current master:
Line 6341 in a9afa4e
| the initialization order is <code>d</code>, <code>b</code>, <code>c</code>, <code>a</code>. |