diff --git a/doc/go_spec.html b/doc/go_spec.html index 98ef599631..158146b9c0 100644 --- a/doc/go_spec.html +++ b/doc/go_spec.html @@ -1,6 +1,6 @@ @@ -6362,16 +6362,16 @@ var t T
-Within a package, package-level variables are initialized in -declaration order but after any of the variables -they depend on. +Within a package, package-level variable initialization proceeds stepwise, +with each step selecting the variable earliest in declaration order +which has no dependencies on uninitialized variables.
More precisely, a package-level variable is considered ready for initialization if it is not yet initialized and either has no initialization expression or -its initialization expression has no dependencies on uninitialized variables. +its initialization expression has no dependencies on uninitialized variables. 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. @@ -6383,6 +6383,23 @@ process ends, those variables are part of one or more initialization cycles, and the program is not valid.
++Multiple variables on the left-hand side of a variable declaration initialized +by single (multi-valued) expression on the right-hand side are initialized +together: If any of the variables on the left-hand side is initialized, all +those variables are initialized in the same step. +
+ ++var x = a +var a, b = f() // a and b are initialized together, before x is initialized ++ +
+For the purpose of package initialization, blank +variables are treated like any other variables in declarations. +
+
The declaration order of variables declared in multiple files is determined
by the order in which the files are presented to the compiler: Variables
@@ -6424,22 +6441,16 @@ or to a function or method that depends on y.
-
-Dependency analysis is performed per package; only references referring -to variables, functions, and methods declared in the current package -are considered. -
-For example, given the declarations
var (
- a = c + b
- b = f()
- c = f()
- d = 3
+ a = c + b // == 9
+ b = f() // == 4
+ c = f() // == 5
+ d = 3 // == 5 after initialization has finished
)
func f() int {
@@ -6450,6 +6461,39 @@ func f() int {
the initialization order is d, b, c, a.
+Note that the order of subexpressions in initialization expressions is irrelevant:
+a = c + b and a = b + c result in the same initialization
+order in this example.
+
+
+
+Dependency analysis is performed per package; only references referring
+to variables, functions, and (non-interface) methods declared in the current
+package are considered. If other, hidden, data dependencies exists between
+variables, the initialization order between those variables is unspecified.
+
+
+
+For instance, given the declarations
+
+
+
+var x = I(T{}).ab() // x has an undetected, hidden dependency on a and b
+var _ = sideEffect() // unrelated to x, a, or b
+var a = b
+var b = 42
+
+type I interface { ab() []int }
+type T struct{}
+func (T) ab() []int { return []int{a, b} }
+
+
+
+the variable a will be initialized after b but
+whether x is initialized before b, between
+b and a, or after a, and
+thus also the moment at which sideEffect() is called (before
+or after x is initialized) is not specified.