Main function and init function
There are two reserved functions in go: the init function (which can be applied to all package) and the main function (can only be applied to package main).
These two functions cannot have any parameters and return values when they are defined. While a package can write any number of Init functions, it is either
For readability or later maintainability, we strongly recommend that users write only one init function per file in a package.
The GO program automatically calls Init () and main (), so you don't need to call these two functions anywhere. The init functions in each package are
Optional, but the package main must contain a main function.
The initialization and execution of the program starts with the main package. If the main package also imports other packages, they are imported sequentially at compile time. Sometimes a
A package is imported at the same time by multiple packages, it will only be imported once (for example, many packages may use the FMT package, but it will only be imported once, because no
It is necessary to import multiple times). When a package is imported, if the package also imports other packages, the other packages are imported before the packages
The package-level constants and variables in the initialization, followed by the Init function (if any), and so on. When all the imported packages are loaded
, the package-level constants and variables in the main package are initialized, then the INIT function in the main package (if present) is executed, and the last
Line main function.
Go language learning-main and init