Skip to content

Commit 467331f

Browse files
authored
Merge pull request #1995 from xushiwei/q
mini spec: prog init & exec
2 parents f71259d + 4eb33a9 commit 467331f

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

doc/spec-mini.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1861,4 +1861,23 @@ The entire package is initialized by assigning initial values to all its package
18611861

18621862
### Program initialization
18631863

1864+
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:
1865+
1866+
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](#package-initialization). This step is repeated until all packages are initialized.
1867+
1868+
Package initialization—variable initialization and the invocation of `init` functions—happens in a single goroutine, sequentially, one package at a time. An `init` function may launch other goroutines, which can run concurrently with the initialization code. However, initialization always sequences the `init` functions: it will not invoke the next one until the previous one has returned.
1869+
1870+
### Program execution
1871+
1872+
A complete program is created by linking a single, unimported package called the main package with all the packages it imports, transitively. The main package must have package name main and declare a function main that takes no arguments and returns no value.
1873+
1874+
```go
1875+
func main() { … }
1876+
```
1877+
1878+
Program execution begins by [initializing the program](#program-initialization) 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.
1879+
1880+
1881+
## Errors
1882+
18641883
TODO

0 commit comments

Comments
 (0)