The implementation principle of Golang import underline

Source: Internet
Author: User

This operation is often confusing for many people an operator, see the following import

Import ("Database/sql" _ "github.com/ziutek/mymysql/godrv")//_ operation is actually the introduction of the package, rather than directly using the function inside the package, but instead called the init function inside the package,//To understand this problem, Take a look at the diagram below to understand how the package is loaded in order:

The initialization and execution of the program starts with the main package. If the main package also imports other packages, they are imported sequentially at compile time. Sometimes a package can be imported at the same time by multiple packages, so it will only be imported once (for example, many packages may use the FMT package, but it will only be imported once, because there is no need to import multiple times). When a package is imported, if the package also imports other packages, the other packages are imported, and then the package-level constants and variables in those packages are initialized, followed by the Init function (if any), and so on. When all the imported packages are loaded, the package-level constants and variables in the main package are initialized, and the INIT function in the main package (if present) is executed, and the main function is finally executed. Explains the entire execution process in detail:

Through the above introduction we understand the import is actually executed the package inside the INIT function, initialized the inside of the variable, _ operation just said that the package introduced, I only initialize the inside of the INIT function and some variables, but often these init functions are registered in their own package inside the engine, So that the external can be easily used, many implementations of Database/sql, in the INIT function is called SQL. Register (name string, driver driver. Driver) Register yourself, and then you can use it externally.

In this way, we will introduce the full import of the situation, I hope you understand the import of Go has some help


The implementation principle of Golang import underline

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.