Commit f4dcf518 authored by Rob Pike's avatar Rob Pike

document initialization

OCL=13369
CL=13369
parent 35b7a174
...@@ -50,10 +50,9 @@ By convention, one package, by default called main, is the starting point for ...@@ -50,10 +50,9 @@ By convention, one package, by default called main, is the starting point for
execution. It contains a function, also called main, that is the first function execution. It contains a function, also called main, that is the first function
invoked by the run time system. invoked by the run time system.
If any package within the program If a source file within the program
contains a function init(), that function will be executed contains a function init(), that function will be executed
before main.main() is called. The details of initialization are before main.main() is called.
still under development.
Source files can be compiled separately (without the source Source files can be compiled separately (without the source
code of packages they depend on), but not independently (the compiler does code of packages they depend on), but not independently (the compiler does
...@@ -2110,9 +2109,40 @@ followed by a series of declarations. ...@@ -2110,9 +2109,40 @@ followed by a series of declarations.
Program = PackageClause { ImportDecl [ ";" ] } { Declaration [ ";" ] } . Program = PackageClause { ImportDecl [ ";" ] } { Declaration [ ";" ] } .
Initialization and Program Execution
----
A package with no imports is initialized by assigning initial values to
all its global variables in declaration order and then calling any init()
functions defined in its source. Since a package may contain more
than one source file, there may be more than one init() function, but
only one per source file.
If a package has imports, the imported packages are initialized
before initializing the package itself. If multiple packages import
a package P, P will be initialized only once.
The importing of packages, by construction, guarantees that there can
be no cyclic dependencies in initialization.
A complete program, possibly created by linking multiple packages,
must have one package called main, with a function
func main() { ... }
defined. The function main.main() takes no arguments and returns no
value.
Program execution begins by initializing the main package and then
invoking main.main().
When main.main() returns, the program exits.
TODO: is there a way to override the default for package main or the
default for the function name main.main?
TODO TODO
---- ----
- TODO: type switch? - TODO: type switch?
- TODO: words about slices - TODO: words about slices
- TODO: really lock down semicolons - TODO: really lock down semicolons
- TODO: need to talk (perhaps elsewhere) about libraries, sys.exit(), etc.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment