Golang Getting Started--the timing of the import package and the in-package Init method

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

Recently Learning Revel (Golang Web Development Framework), learn how Revel manages and loads all controllers. The basic knowledge involved in Golang is the import package. Let's take a look at several methods and features of the Golang Imort package:

The first way relative to the path

Import   "./module"   //The module directory of the same directory as the current file, this method is not easy to use error </span>

The second way absolute path

import "Learngo/init" //Load Gopath/src/learngo/init module

Some special import methods are shown below

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 the fmt.Println("hello world") one you called earlier can be omitted as writtenPrintln("hello world")

2. Alias operation

Alias operation as the name implies, we can name the package another one that we use to remember easily, and the Revel Framework's app/controllers/tmp/main.go (the frame's start-up portal) can be seen in this way.

Import (    F "FMT")

Alias operation the prefix becomes our prefix when calling the package function, i.e.f.Println("hello world")。


Import (Code snippet for//revel framework "flag" "Reflect" "Github.com/revel/revel" Controllers0 "github.com/revel/modules/static/app/ Controllers "_" Github.com/revel/modules/testrunner/app "Controllers1" github.com/revel/modules/testrunner/app/ Controllers "_" Guild_website/app "Controllers" Guild_website/app/controllers "tests" guild_website/tests "" Github.com/revel/revel/testing ")

3._ operation

This operation is often confusing for many people an operator, see the following import

Import (Code snippet for//revel Framework _ "Github.com/revel/modules/testrunner/app" _ "Guild_website/app")

_ 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. It is also necessary to understand how the alias operation Import Package will execute the INIT function .









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.