This is a creation in Article, where the information may have evolved or changed.
See example code in turn:
P1.go
PackageP1Import"FMT"//1.1varXfloat32=1.2//1.2funcInit() {//1.3 Fmt.Printf("P1 package, x:%f\n", X//1.4}funcDonothing() {FMT.Println("Do nothing.\n")}
A.go:
PackageMainImport"FMT"varWhatIsThe1 =Answertolife(2.1)//2.1varWhatIsThe2 =Answertolife(2.2)//2.2varWhatIsThe3 =Answertolife(2.3)//2.3funcInit() {//3.1 Fmt.Printf("Init whatisthe in A.go ' s init 3.1:%d\n", 2)}funcInit() {//3.2 Fmt.Printf("Init whatisthe in A.go ' s init 3.2:%d\n", 3)}
Testinit.go
PackageMainImport"P1"//1Import"FMT"varWhatIsThe4 =Answertolife(2.4)//2.4varWhatIsThe5 =Answertolife(2.5)//2.5varWhatIsThe6 =Answertolife(2.6)//2.6funcAnswertolife(Indexfloat32)float32{FMT.Printf("init package level variable, whatisthe:%f\n", IndexreturnIndexfuncInit() {//3.3 Fmt.Printf("Init whatisthe in Testinit.go ' s init3.3:%d\n", 0)}funcInit() {//3.4 Fmt.Printf("Init whatisthe in Testinit.go ' s init3.4:%d\n", 1)}funcMain() {//4 P1. Donothing ()//5}
Z.go
PackageMainImport"FMT"varWhatIsThe7 =Answertolife(2.7)//2.7varWhatIsThe8 =Answertolife(2.8)//2.8varWhatIsThe9 =Answertolife(2.9)//2.9funcInit() {//3.5 Fmt.Printf("Init whatisthe in Z.go ' s init 3.5:%d\n", 4)}funcInit() {//3.6 Fmt.Printf("Init whatisthe in Z.go ' s init 3.6:%d\n", 5)}
The order in which the code files are posted is the sequence of initialization between the chunks, the exact order of the shapes, such as//1,//2.1 and so on, the values from small to large, the first initialization, in turn.
Summary: In a go file, the initialization sequence rules: (1) the introduced package (2) variable constants in the current package (3) the Init (4) Main function of the current package
Attention:
0. In the current go source file, each import package is initialized in the order in which it appears in the source file.
1. If the current package has more than one init in different source files, then the source file name is sorted from small to large in the dictionary order, the smaller is executed first, the same package and Init in the same source file are initialized sequentially in the order in which they appear in the file; package The level variable constant also follows this rule; in fact, it should be in the order of the source file names submitted to the compiler, just before the compiler is submitted, the GO Command line tool sorts the source filenames by dictionary order.
2. Init can only be called by Go runtine, we cannot display the call in the code, nor can it be referenced, as assigned to a function variable.
3. Package A introduces package B, package B introduces package C, and the package is initialized in the following order: C, B, A
4. The introduction of the package must avoid a dead loop, such as a-lead B, B-citation C, C-lead a.
5. A package is introduced by a number of other packages, such as C, a, B->c and H, C, which are cited 2 times by the other packages, but note that package C is initialized only once.
6. Another big principle, is always initialized first, of course.
7. The main package is always initialized by the last one, which is well understood because it always relies on other packages.
Code in: Https://github.com/yujinliang/test_init_sequence
Reference
Http://stackoverflow.com/questions/17733220/go-package-initialization
Https://golang.org/ref/spec#Program_initialization_and_execution
Http://stackoverflow.com/questions/24790175/when-is-the-init-function-in-go-golang-run
This image is quoted from: "Go language Programming", Mark Summerfield, People's post and telecommunications press
Note: This article is only my personal notes, if there are errors and omissions, please correct me, study together, my e-mail: htyu_0203_39@sina.com