Getting Started with Golang import

Source: Internet
Author: User
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:

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

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 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.

Import (    "FMT")

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

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.