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:
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:
1. Relative path
Import "./model"//The Model directory of the same directory as the current file, but it is not recommended to import this way
2. 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
1. Point operation
We sometimes see the following ways to import packages
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 what you can omit from the previous call. fmt.Println("hello world")
Println("hello world")
2. Alias operation
Alias operation as the name implies, we can name the package another one that we remember easily.
Alias operation the prefix becomes our prefix when calling the package function, i.e.f.Println("hello world")
3._ 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, instead of directly using the function inside the package, but called the INIT function inside the package, to understand this problem, you need to look at the following diagram 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:
Golang Import
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 convenient to use, a lot of implementation of the Database/sql, in the Init function is called to sql.Register(name string, driver driver.Driver)
register themselves, and then the external can be used.
In this way we will introduce the full import of the situation, I hope you understand the import of Go has some help.
Http://0x55aa.sinaapp.com/%E7%AE%97%E6%B3%95-%E7%BC%96%E7%A8%8B/840.html