From 8cb9184d7ff869ad4ddd3174cb301bc88db15178 Mon Sep 17 00:00:00 2001 From: Rob Pike Date: Tue, 15 Sep 2009 11:56:39 -0700 Subject: [PATCH] an attempt to define initialization order within a package. DELTA=23 (19 added, 1 deleted, 3 changed) OCL=34646 CL=34649 --- doc/go_spec.html | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/doc/go_spec.html b/doc/go_spec.html index d783a2e0afd..abe26fc4192 100644 --- a/doc/go_spec.html +++ b/doc/go_spec.html @@ -764,7 +764,7 @@ a type named T: A field declaration may be followed by an optional string literal tag, which becomes an attribute for all the identifiers in the corresponding field declaration. The tags are made -visible through a reflection library TODO: reference? +visible through a reflection interface but are otherwise ignored.

@@ -2384,7 +2384,7 @@ its dynamic type is a structure whose sequential fields are the trailing arguments of the call. That is, the actual arguments provided for a ... parameter are wrapped into a struct that is passed to the function instead of the actual arguments. -Using the reflection library (TODO: reference), f may +Using the reflection interface, f may unpack the elements of the dynamic type to recover the actual arguments.

@@ -4281,8 +4281,7 @@ var t T

Program execution

A package with no imports is initialized by assigning initial values to -all its package-level variables in data-dependency order -(TODO: clarify) +all its package-level variables and then calling any package-level function with the name and signature of

@@ -4296,6 +4295,25 @@ than one source file, there may be more than one only one per source file.

+Within a package, package-level variables are initialized, +and constant values are determined, in +data-dependent order: if the initializer of A +depends on the value of B, A +will be set after B. +It is an error if such dependencies form a cycle. +Dependency analysis is done lexically: A +depends on B if the value of A +contains a mention of B, contains a value +whose initializer +mentions B, or mentions a function that +mentions B, recursively. +If two items are not interdependent, they will be initialized +in the order they appear in the source. +Since the dependency analysis is done per package, it can be +defeated if A's initializer calls a function defined +in another package that refers to B. +

+

Initialization code may contain "go" statements, but the functions they invoke do not begin execution until initialization of the entire program is complete. Therefore, all initialization code is run in a single