Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the function of init function in Go language

2025-03-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you what the function of init function in Go language is. The content is concise and easy to understand. It can definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

The Go language has a special function init, which executes before the main function and implements some initialization operations at the package level.

For init functions: each package can contain any number of init functions that are called at the beginning of program execution. All init functions discovered by the compiler are scheduled to execute before the main function. The init function is used to set up packages, initialize variables, or other boot work to be done first before the program runs.

The main functions of the init function are: 1) initialize variables that cannot be initialized with initialization expressions. 2) Registration before the program is run. 3) realize the function of sync.Once. Note: the init function has no input parameters and returns a value. )

1. Execution order of init functions in a single package

In the GE language, the order of execution is: initialize variables-- "init function--" main function.

Result analysis: by outputting the results, we can see that before starting to run, Go programs first perform variable initialization, then the init () function, and finally the main () function.

two。 If there are multiple init functions in a single package, their execution order is uncertain.

In the Go language, multiple init () functions can be implemented in a package file and a .go file without compilation errors.

Result analysis: through the output results, we can see that multiple init functions in the same package can be implemented, in theory, their execution order can be different, but the author executed 5 times is the result of the above, I do not know whether it is the reason of the compilation order.

3. Init functions between interdependent packages, executed in recursive order

As shown in the following figure:

(reprint: https://blog.csdn.net/benben_2015/article/details/79486077)

Examples are as follows:

Result analysis: the dependency of the package main- > b-> a, so the init function of the output result is executed first in a, then in b, and finally in c.

4. Additional knowledge:

Go is very strict and does not allow references to packages that are not in use, but we can refer to them in the following format (import _ pkg), which is just used to execute the init function of the corresponding package.

Replace the import ". / b" in the above c.go with import _ ". / b", and delete the b.B () in main (), as shown below.

The above is what is the function of init function in Go language. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report