Skip to content

Commit ee5af61

Browse files
committed
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 <khr@google.com> Reviewed-by: Robert Griesemer <gri@google.com> TryBot-Bypass: Robert Griesemer <gri@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com> Reviewed-by: Keith Randall <khr@golang.org>
1 parent 886fba5 commit ee5af61

File tree

1 file changed

+25
-14
lines changed

1 file changed

+25
-14
lines changed

doc/go_spec.html

+25-14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<!--{
22
"Title": "The Go Programming Language Specification",
3-
"Subtitle": "Version of June 7, 2023",
3+
"Subtitle": "Version of June 13, 2023",
44
"Path": "/ref/spec"
55
}-->
66

@@ -8003,6 +8003,9 @@ <h3 id="Package_initialization">Package initialization</h3>
80038003
by the order in which the files are presented to the compiler: Variables
80048004
declared in the first file are declared before any of the variables declared
80058005
in the second file, and so on.
8006+
To ensure reproducible initialization behavior, build systems are encouraged
8007+
to present multiple files belonging to the same package in lexical file name
8008+
order to a compiler.
80068009
</p>
80078010

80088011
<p>
@@ -8113,15 +8116,30 @@ <h3 id="Package_initialization">Package initialization</h3>
81138116
</p>
81148117

81158118
<p>
8116-
A package with no imports is initialized by assigning initial values
8117-
to all its package-level variables followed by calling all <code>init</code>
8118-
functions in the order they appear in the source, possibly in multiple files,
8119-
as presented to the compiler.
8119+
The entire package is initialized by assigning initial values
8120+
to all its package-level variables followed by calling
8121+
all <code>init</code> functions in the order they appear
8122+
in the source, possibly in multiple files, as presented
8123+
to the compiler.
8124+
</p>
8125+
8126+
<h3 id="Program_initialization">Program initialization</h3>
8127+
8128+
<p>
8129+
The packages of a complete program are initialized stepwise, one package at a time.
81208130
If a package has imports, the imported packages are initialized
81218131
before initializing the package itself. If multiple packages import
81228132
a package, the imported package will be initialized only once.
81238133
The importing of packages, by construction, guarantees that there
81248134
can be no cyclic initialization dependencies.
8135+
More precisely:
8136+
</p>
8137+
8138+
<p>
8139+
Given the list of all packages, sorted by import path, in each step the first
8140+
uninitialized package in the list for which all imported packages (if any) are
8141+
already initialized is <a href="#Package_initialization">initialized</a>.
8142+
This step is repeated until all packages are initialized.
81258143
</p>
81268144

81278145
<p>
@@ -8135,13 +8153,6 @@ <h3 id="Package_initialization">Package initialization</h3>
81358153
until the previous one has returned.
81368154
</p>
81378155

8138-
<p>
8139-
To ensure reproducible initialization behavior, build systems are encouraged
8140-
to present multiple files belonging to the same package in lexical file name
8141-
order to a compiler.
8142-
</p>
8143-
8144-
81458156
<h3 id="Program_execution">Program execution</h3>
81468157
<p>
81478158
A complete program is created by linking a single, unimported package
@@ -8157,8 +8168,8 @@ <h3 id="Program_execution">Program execution</h3>
81578168
</pre>
81588169

81598170
<p>
8160-
Program execution begins by initializing the main package and then
8161-
invoking the function <code>main</code>.
8171+
Program execution begins by <a href="#Program_initialization">initializing the program</a>
8172+
and then invoking the function <code>main</code> in package <code>main</code>.
81628173
When that function invocation returns, the program exits.
81638174
It does not wait for other (non-<code>main</code>) goroutines to complete.
81648175
</p>

0 commit comments

Comments
 (0)