1
1
<!--{
2
2
"Title": "The Go Programming Language Specification",
3
- "Subtitle": "Version of June 7 , 2023",
3
+ "Subtitle": "Version of June 13 , 2023",
4
4
"Path": "/ref/spec"
5
5
}-->
6
6
@@ -8003,6 +8003,9 @@ <h3 id="Package_initialization">Package initialization</h3>
8003
8003
by the order in which the files are presented to the compiler: Variables
8004
8004
declared in the first file are declared before any of the variables declared
8005
8005
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.
8006
8009
</ p >
8007
8010
8008
8011
< p >
@@ -8113,15 +8116,30 @@ <h3 id="Package_initialization">Package initialization</h3>
8113
8116
</ p >
8114
8117
8115
8118
< 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.
8120
8130
If a package has imports, the imported packages are initialized
8121
8131
before initializing the package itself. If multiple packages import
8122
8132
a package, the imported package will be initialized only once.
8123
8133
The importing of packages, by construction, guarantees that there
8124
8134
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.
8125
8143
</ p >
8126
8144
8127
8145
< p >
@@ -8135,13 +8153,6 @@ <h3 id="Package_initialization">Package initialization</h3>
8135
8153
until the previous one has returned.
8136
8154
</ p >
8137
8155
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
-
8145
8156
< h3 id ="Program_execution "> Program execution</ h3 >
8146
8157
< p >
8147
8158
A complete program is created by linking a single, unimported package
@@ -8157,8 +8168,8 @@ <h3 id="Program_execution">Program execution</h3>
8157
8168
</ pre >
8158
8169
8159
8170
< 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 > .
8162
8173
When that function invocation returns, the program exits.
8163
8174
It does not wait for other (non-< code > main</ code > ) goroutines to complete.
8164
8175
</ p >
0 commit comments