This is a creation in Article, where the information may have evolved or changed.
The init function in the Go language is used for the initialization of the package, which is an important feature of the Go language,
Has the following characteristics:
1 The INIT function is a function that initializes the package before the program executes, such as initializing the variables in the package.
2 Each package can have more than one init function
3 each source file of a package can also have multiple init functions
4 execution order of multiple Init functions in the same package the go language has no explicit definition (description)
5 The Init function of the different packages determines the order of execution of the initialization function according to the dependency of the package import
6 The Init function cannot be called by another function, but is called automatically before the main function executes.
The following example is excerpted from the "The Go", where OS differences are hidden when the application is initialized.
varprompt ="Enter a digit, e.g. 3"+"or%s to quit."func init () {ifRuntime. GOOS = ="Windows"{Prompt= Fmt. Sprintf (Prompt,"Ctrl + Z, Enter") } Else{//Unix-likeprompt = Fmt. Sprintf (Prompt,"Ctrl+d") }}
The following two go files demonstrate:
11 package or go files can contain multiple init functions,
2 The INIT function is executed before the main function,
3 The INIT function is called automatically and cannot be called in other functions, and an explicit call will report that the function is undefined
Gprog.go Code
Package Mainimport ("FMT")//The other init function in this go source filefunc init () {FMT. Println ("Do in init")}func Main () {FMT. Println ("Do in main")}func testf () {fmt. Println ("Do in testf") //if uncomment the next statment, then go to build give error message:. \gprog.go:19:undefined:init//Init ()}
Ginit1.go code, note that there are two init functions in this source file
Package Mainimport ( "fmt")// The first init function In this go source filefunc init () { fmt. Println ("do ininit1")}// The second init function In this go source filefunc init () { fmt. Println ("do ininit2")}
Compile the above two files: Go build gprog.go ginit1.go
The result of executing Gprog.exe after compilation shows that the Init function in Gprog.go executes first and then executes the two init functions in ginit1.go before executing the main function.
e:\opensource\go\prj\hellogo>gprog.exe do in init does in init1 Do inch Init2 Do in Main
Note: "The Way to Go" (P70) has the following red sentence description, meaning that a go source file can only have one init function,
However, the two init functions in the above ginit1.go are executed normally after they have been compiled and run.
So this sentence should be a clerical error.
4.4.5init-Functionsapart from GlobalDeclaration with initialization, variables can also be initializedinchAn init ()-function. this isA special function with the name init () which cannot is called, but isexecuted Automaticallybefore the main () functioninchPackage main or at the start of the import of the thatcontains it. every source file can contain only1 init ()-function. Initialization isAlways single-threaded andpackage Dependency guarantees correct execution order.
2013.04.21 First Draft
2013.04.23 adds a typo in the "The Go" on the init function