Golang Package principle

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

1, the first is the introduction of the principle of the package

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.

2. Package Import Syntax

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

import(    "fmt")``然后在代码里面可以通过如下的方式调用

Fmt. Println ("Hello World")

上面这个fmt是Go语言的标准库,他其实是去GOROOT下去加载该模块。## 1. 点操作   

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"). Note: The functions within the package cannot have their names.

2. Alias operation

import( f “fmt” ) 

The alias operation, as the name implies, can name the package another easy-to-remember moniker, and the alias operation calls the packet function with the prefix changed to the renamed prefix, or F. Println ("Hello World").

3. _ Operation

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

import (    _ "github.com/go-sql-driver/mysql") 

_ 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 ().

3, Package problems

Http://blog.csdn.net/cmbug/ar ... Cite a relatively good article.

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.