Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
preface
We all know that golang’s entry function is the main function, where some initialization is done.
Today we’ll look at another special golang function, init(), which also performs some initialization operations. Let’s take a closer look at the properties of the init() function.
Introduction to init Function
Go defines the init function as follows:
The init function
Finally, each source file can define its own niladic init function to set up whatever state is required. (Actually each file can have multiple init functions.) And finally means finally: init is called after all the variable declarations in the package have evaluated their initializers, and those are evaluated only after all the imported packages have been initialized.
Besides initializations that cannot be expressed as declarations, a common use of init functions is to verify or repair correctness of the program state before real execution begins.
From the above information, we can learn that:
-
Each source file can define an init() function that performs some pre-run work;
-
Each file can contain multiple init() functions;
-
The init() function does not run until all global variables in a package have been initialized;
Init function features
Now that we know what the init() function does, let’s look at some of the features of the init() function.
There are multiple init functions
Actually each file can have multiple init functions.
Not only can there be multiple init() functions in the same package, but there can also be multiple init() functions in the same source file.
The life cycle
init is called after all the variable declarations in the package have evaluated their initializers, and those are evaluated only after all the imported packages have been initialized.
From the above we can see that the init() function starts to run after all global variables in a package have been initialized.
This feature is great for organizing your code.
There must be many source files in one of our packages. We can write the init() function related to the source file in the corresponding file for easy understanding and management.
There are no input and output parameters
each source file can define its own niladic init function to set up whatever state is required.
From the introduction above, we can see that the init() function is a niladic function.
a niladic function is a special type of function that takes no arguments and is evaluated immediately when it is encountered in a statement.
A niladic function is a function that has no input or output.
When we add parameters to an init() function, the compiler prompts us with the following error message:
Function init must have no arguments and no return values
Copy the code
Execution order
First the conclusion:
-
The init() function precedes the main() function;
-
If there are multiple init() functions in the same file, execute them from the top down;
-
Multiple files are executed in lexicographical order;
Test9; TestA; TestB; Handler;
/ / the main method
func main(a) {
fmt.Println("func main run")}Test9 class init method
func init(a) {
fmt.Println("test9 init1 run")}// TestA init method
func init(a) {
fmt.Println("testA init1 run")}func init(a) {
fmt.Println("testA init2 run")}// TestB init method
func init(a) {
fmt.Println("testB init1 run")}Copy the code
The running results are as follows:
test9 init1 run
testA init1 run
testA init2 run
testB init1 run
func main run
Copy the code
And as you can see, it’s consistent with what we said.
As a side effect
Sometimes, we want to call the init() method in a package, but we don’t want to show that the package is used, we usually do this, using the _ symbol to rename it.
import _ "net/http/pprof"
Copy the code
This is a common use of the init() function.
role
Now let’s summarize what init() does:
-
Init () aims to implement some package-level initialization operations;
-
Initialize a function that cannot be initialized with an initialization expression;
-
Realize sync.once function (singleton mode);
-
Pre-run check/repair status;
Reference documentation
-
Golang init function in 5 minutes
-
Init official documentation
-
Explain the init () function in Go
-
Golang init function