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