Let's start directly from the two Go language applet: * * program 1** ' gopackage mainimport "FMT" var (a int = b + 1b int = 1) func main () {FMT. Println (a) fmt. Println (b)} "* * program 2**" "Gopackage mainimport" FMT "Func Main () {var (a int = b + 1b int = 1) fmt. Println (a) fmt. Println (b)} "if these two pieces of code output the same result, then they are not good material, fortunately, their results are different: * * program 1** ' 21" * * Program 2** This program can not be compiled, and even in the 7th line to report a compile-time error " Undefined:b ". What is it that has brought about this discrepancy? The "normal" initialization expression in the variable declaration, initialized from left to right and top to bottom as you expect: ' ' Gofunc f () int {fmt. Println ("F"); Return 1}func g () int {fmt. Println ("G"); Return 2}func h () int {fmt. Println ("H"); Return 3}func Main () {var (a int = f () b int = g () c int = h ()) Fmt. Println (A, B, c)} "output:" FGH1 2 3 "" normal "means that it is initialized in its own function. When these initialization code is placed in the top-level declaration of a package as in Program 1, it becomes more and more interesting: "' Gopackage mainimport" FMT "var (a = C-2B = 2c = f ()) func f () int {fmt. PRINTF ("inside f and B =%d\n", b) return B + 1}func main () = FMT. Println (a) fmt. Println (b) fmt. Println (c)} "' These variables are declared in the following order: * B is the first because it does not depend on other uninitialized variables * C is the second, after initialization of the variable B required by the ' F ' function, followed by initialization * A is the output of the handler in the third round initialization loop after C is initialized For: "' Inside F andb = 2123 "in the order of declaration, each initialization process selects the first variable that can be initialized. The entire process continues until all variables are initialized or the compiler finds a loop similar to the following: "' Gopackage mainimport" FMT "var (a = BB = CC = f ()) func f () int {return A}func main () {FMT. The code above PRINTLN (A, B, c)} "will cause the compile-time error" initialization loop ". The initialization dependency mechanism works based on the package level: **sandbox.go** "Gopackage mainimport" FMT "var (a = C-2B = 2) func main () {FMT. Println (a) fmt. Println (b) fmt. Println (c)} "**utils.go**" "gopackage mainvar C = f () func f () int {return B + 1} ' ' Compile and output: ' ' 123 ' if you like the above, please follow me, To accelerate the development of future stories.
via:https://medium.com/golangspec/initialization-dependencies-in-go-51ae7b53f24c
Author: Michałłowicki Translator: Rxcai proofreading: polaris1119
This article by GCTT original compilation, go language Chinese network honor launches
This article was originally translated by GCTT and the Go Language Chinese network. Also want to join the ranks of translators, for open source to do some of their own contribution? Welcome to join Gctt!
Translation work and translations are published only for the purpose of learning and communication, translation work in accordance with the provisions of the CC-BY-NC-SA agreement, if our work has violated your interests, please contact us promptly.
Welcome to the CC-BY-NC-SA agreement, please mark and keep the original/translation link and author/translator information in the text.
The article only represents the author's knowledge and views, if there are different points of view, please line up downstairs to spit groove
329 Reads