go program sequence (rpm)

In one go program generally comprises: a packet, constants, variables, init (), main () and other elements, if there are multiple packages at the same time, dependency exists between packets, the plurality of the init function is present in each packet, each there are multiple files init function, then the question is, the execution order between them is what? In this article we have to do as a detailed description of the execution order between them. If not correct, welcome criticism.

Execution order package

  • go file in the main package default will always be executed
  • Go different files in the same package, "small to large" to perform the sort order by file name
  • Other packages will import only the main pack are performed in accordance with the order of import
  • Is recursively import packet initialization sequence and import reverse order, for example: Import order main -> A -> B -> C, the initialization sequence is C -> B -> A -> main
  • Coating a plurality of other packets import, but can be initialized only once
  • main bag is always the last initialization, because it is always dependent on other packages
  • Avoid circulation import, for example: A - A> -> B -> C

Go program initialization sequence shown below: 
Write pictures described here

and the main function init

init (), main () is retained in the language go, the differences in the two functions in the language go as follows: 
the same point:

  • When two function definition can not have any parameters and return values
  • This function can only be called automatically by the program go, it can not be cited

difference:

  • init can be applied to any package, and may be repeated a plurality of definitions.
  • The main function can be used only in the main bag, and only a defined.

The order of execution of two functions:

  • Call init order to go the same file () is from top to bottom
  • For the same package in a different file, the file name will be "small to large" order, after calling each file init sequence () function by string
  • Package for a different, if not dependent, then call its init package in order to import the package main () function
  • If there is a dependency package, the calling sequence for the last dependent first be initialized, for example: Import order main -> A -> B -> C, the initialization sequence is C -> B -> A -> main, corresponding to the first performance the init method.

Constants, variables, init (), main ()

In the same file, constants, variables, init (), main () in order to initialize.

init sequence

1, in the same package, the method can be defined a plurality of files init

2, go in the same file may be repeated init method defined

3, in the same package, do the init method of different files by file name has the init method for each file

4, a plurality of the init method in the same file, performing different init method sequentially in the order written in the code

5, for different Package, if it is not interdependent, which calls init package in order to import the package main () function

6, if there is a dependency package, last call order is first to be initialized dependent, for example: Import order main -> A -> B -> C, the initialization sequence is C -> B -> A -> main, primary init performs the corresponding method.

Perform all init functions in the same ⼀ a goroutine. 
After all the init function will Perform main.main function.

 

Reproduced

Guess you like

Origin www.cnblogs.com/tomhuang/p/11619787.html