The init function in go language is used to initialize the package. This function is an important feature of the go language.
has the following Characteristics:
1 The init function is a function used to initialize the package before program execution, such as initializing variables in the package, etc.
2 Each package can have multiple init functions
3 Each source file of a package can also have multiple init functions
4 The execution order of multiple init functions in the same package is not clearly defined (instructions) in the Go language
5 The init functions of different packages determine the execution order of the initialization functions according to the dependencies imported by the package.
6 The init function cannot be called by other functions, but is automatically called before the main function is executed.
The following example is excerpted from "the way to go". The OS differences are hidden during application initialization.
var prompt = "Enter a digit, e.g. 3 " + "or %s to quit." func init() { if runtime.GOOS == "windows" { prompt = fmt.Sprintf(prompt, "Ctrl+Z, Enter") } else { // Unix-like prompt = fmt.Sprintf(prompt, "Ctrl+D") } }
The following two go files demonstrate:
1 A package Or the go file can contain multiple init functions.
2 The init function is executed before the main function.
3 The init function is automatically called and cannot be called in other functions. Explicitly The call will report that the function is undefined
gprog.go code
package main import ( "fmt" ) // the other init function in this go source file func 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 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 main import ( "fmt" ) // the first init function in this go source file func init() { fmt.Println("do in init1") } // the second init function in this go source file func init() { fmt.Println("do in init2") }
Compile the above two File: go build gprog.go ginit1.go
The result of executing gprog.exe after compilation shows that the init function in gprog.go is executed first, and then the two init functions in ginit1.go are executed. Then the main function is executed.
E:\opensource\go\prj\hellogo>gprog.exe do in init do in init1 do in init2 do in main
Note: In "The Way to Go" (P70), there is a description in red below, which means that a go source file can only have one init function,
but the above ginit1. The two init functions in go are executed normally after compilation and running,
, so this sentence should be a typo.
4.4.5 Init-functions Apart from global declaration with initialization, variables can also be initialized in an init()-function. This is a special function with the name init() which cannot be called, but is executed automatically before the main() function in package main or at the start of the import of the package that contains it. Every source file can contain only 1 init()-function. Initialization is always single-threaded and package dependency guarantees correct execution order.
Recommended: go video tutorial
The above is the detailed content of Detailed explanation of init function in go language. For more information, please follow other related articles on the PHP Chinese website!