This is a creation in Article, where the information may have evolved or changed.
When we write the go code, we often use the Import command for importing package files, which we often see in the following way:
Import ( "FMT")
Then we can call the code in the following way
Fmt. Println ("Hello World")
Above this fmt is the Go Language standard library, he actually went to goroot down to load the module, of course, the import of Go also supports the following two ways to load their own modules:
relative path
import "./model"// The model directory of the same directory as the current file, but it is not recommended to import
absolute path
import "Shorturl/model"// Load Gopath/src/shorturl/model module
The above shows some of the import commonly used in several ways, but there are some special import, so many novice very difficult to understand, let's one by one to explain what is the matter
- Point operation
We sometimes see the following ways to import packages
Import (. "FMT") this point operation means that after the package is imported, you can omit the prefix's package name when you call the package's function, which is the FMT you called earlier. Println ("Hello World") can be omitted as written Println ("Hello World")
- Alias operation
Alias operation as the name implies, we can name the package another one that we remember easily.
Import (F "FMT") alias operation the prefix becomes our prefix when calling the package function, that is, F. Println ("Hello World")
- _ Operation
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.
Original address: http://blog.beego.me/blog/2013/07/27/golang-import-shi-yong-ru-men/