Golang-import the syntax for importing packages

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Import syntax for a package

When you write the go code, you often use the Import command for importing package files, as shown in the following ways:

Import
"FMT"
)

The code can then be called 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 not recommended in this way import

Absolute path import "Shorturl/model"//Load Gopath/src/shorturl/model module

The above shows some import commonly used in several ways, but there are some special import, so many novice very confusing, the following is the use of three kinds of import package.


1. The point operation will sometimes see the following way of importing the package import (. "FMT")
The 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")
2. alias Operation alias operation as the name implies, the package can be named as another easy to remember names.
The import (f "FMT") alias operation invokes the package function when the prefix becomes a renamed prefix, that is, F. Println ("Hello World")
3. _ Operation This operation is often confusing to many people an operator, see the following import
Import ("Database/sql" _ "Github.com/ziutek/mymysql/godrv")

_ Operation actually just introduces the package. When a package is imported, all of its init () functions are executed, but sometimes it is not really necessary to use these packages, just want their init () function to be executed. This time you can use the _ operation to reference the package. Even if you use the _ operation to reference a package, you cannot call the exported function in the package by the package name, but simply call its init function ().

Two-pack Import process description

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 in the package of the INIT function, initialized the inside of the variables, _ operation just said that the package introduced, only initialize the inside of the INIT function and some variables, can not be called by the package name of other functions, what is the use of it? Often these init functions inside of the registration of 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 SQL. Register (name string, driver driver. Driver) Register yourself, and then you can use it externally.

Related Article

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.