From: Robert Griesemer Date: Wed, 7 Jun 2023 23:56:28 +0000 (-0700) Subject: spec: document new program initialization process X-Git-Tag: go1.21rc1~20 X-Git-Url: http://www.git.cypherpunks.ru/?a=commitdiff_plain;h=ee5af6103da43c44104b1d06eaf5a23cd9b87085;p=gostls13.git spec: document new program initialization process For #57411. Change-Id: I94982d939d16ad17174f801cc167cc10ddc8da30 Reviewed-on: https://go-review.googlesource.com/c/go/+/501696 Reviewed-by: Keith Randall Reviewed-by: Robert Griesemer TryBot-Bypass: Robert Griesemer Reviewed-by: Ian Lance Taylor Reviewed-by: Keith Randall --- diff --git a/doc/go_spec.html b/doc/go_spec.html index bb4a3f600c..bb5b2f3db9 100644 --- a/doc/go_spec.html +++ b/doc/go_spec.html @@ -1,6 +1,6 @@ @@ -8003,6 +8003,9 @@ The declaration order of variables declared in multiple files is determined by the order in which the files are presented to the compiler: Variables declared in the first file are declared before any of the variables declared in the second file, and so on. +To ensure reproducible initialization behavior, build systems are encouraged +to present multiple files belonging to the same package in lexical file name +order to a compiler.

@@ -8113,15 +8116,30 @@ in a program.

-A package with no imports is initialized by assigning initial values -to all its package-level variables followed by calling all init -functions in the order they appear in the source, possibly in multiple files, -as presented to the compiler. +The entire package is initialized by assigning initial values +to all its package-level variables followed by calling +all init functions in the order they appear +in the source, possibly in multiple files, as presented +to the compiler. +

+ +

Program initialization

+ +

+The packages of a complete program are initialized stepwise, one package at a time. If a package has imports, the imported packages are initialized before initializing the package itself. If multiple packages import a package, the imported package will be initialized only once. The importing of packages, by construction, guarantees that there can be no cyclic initialization dependencies. +More precisely: +

+ +

+Given the list of all packages, sorted by import path, in each step the first +uninitialized package in the list for which all imported packages (if any) are +already initialized is initialized. +This step is repeated until all packages are initialized.

@@ -8135,13 +8153,6 @@ the init functions: it will not invoke the next one until the previous one has returned.

-

-To ensure reproducible initialization behavior, build systems are encouraged -to present multiple files belonging to the same package in lexical file name -order to a compiler. -

- -

Program execution

A complete program is created by linking a single, unimported package @@ -8157,8 +8168,8 @@ func main() { … }

-Program execution begins by initializing the main package and then -invoking the function main. +Program execution begins by initializing the program +and then invoking the function main in package main. When that function invocation returns, the program exits. It does not wait for other (non-main) goroutines to complete.