Go language - pack (package)

Go in the engineering of language development projects, Go language source code reuse is built on the package (package) foundation. This article describes how to define the language pack Go, how to export the contents of the package and how to import other packages.

Go language package (package)

Package Introduction

包(package)Go is a collection of source code, is an advanced code reuse program, Go language provides us with a lot of built-in packages, such as fmt, os, ioand so on.

Custom packages

We can also create your own package according to their needs. A package can be simply interpreted as a stored .godocument files. The folder following documents must all go add the following code in the first line of code, stating that the file belongs package.

package 包名

Precautions:

  • Folder contains a file directly below only a homepackage , a same packagefile in multiple folders can not.
  • Package name and the name of the folder may not be the same as the package name can not contain  - symbols.
  • Package called mainbag of entry packet of the application , after which the package will be a compiled executable file, the compiler does not contain mainthe source package you will not get an executable file.

Visibility

If you want to reference a package in a package when additional identifier (e.g., variables, constants, types, functions, etc.), the identifier must be visible outside (public). Go first letter in the language identifier only need to make an identifier of foreign capital can be visible.

For example, we define a package called pkg2bag, as follows:

pkg2 Package 

Import "FMT" // package variable visibility var A = 100 // first letter in lower case, not visible outside the package, the package can only be used within the current 
// initials visible outer cladding, may be used in other packages const =. 1 MODE 
type Person struct { // first letter in lower case, not visible outside the package, the package can only be used within the current 
    name String 
} // initials, visible outer cladding, may be used in other packages 
FUNC the Add (X, Y int) {int
     return X + Y 
} 
FUNC Age () { // first letter in lower case, not visible outside the package, the package can only be used within the current var Age = 18 is // function local variables, not visible outside the package, only function of the current provided with 
    FMT. Println (Age) 
}










    

Struct field and method names interface if the first letter is capitalized, external package can access these fields and methods . E.g:

Student {struct type 
    the Name   String  // accessible outside the package method of 
    class  String  // field is limited access to the inner cladding 
} 

type Payer interface { 
    the init () // only way to access the inner pack 
    the Pay ()   // may the method of the outer package access 
}

Introducing the package

To refer to the contents of other packages in your code, you use importthe keyword into the use of the package. Specific syntax is as follows:

import "route package"

Precautions:

  • import import statements are usually placed at the beginning of the following package declaration.
  • Introducing the package name use double quotation wrapped.
  • Packages are from$GOPATH/src/ the start of the calculation, using /a route separated.
  • Go language prohibiting circulation import package.

Importing a single line

Introducing single-line format is as follows:

import "包1"
import "包2"

Multi-line imports

Import multi-line format is as follows:

import (
    "包1"
    "包2"
)

Custom package name

When importing the package name, we can also import package settings alias. Conflict situations package name commonly used for imported package name is too long or imported. Specific syntax is as follows:

import alias "route package"

Introducing single-line manner to define an alias:

import "fmt"
import m "github.com/Q1mi/studygo/pkg_test"

func main() {
    fmt.Println(m.Add(100, 200))
    fmt.Println(m.Mode)
}
多行导入方式定义别名:

import (
    "fmt"
    m "github.com/Q1mi/studygo/pkg_test"
 )

func main() {
    fmt.Println(m.Add(100, 200))
    fmt.Println(m.Mode)
}

Anonymous Import Package

If you only want to import the package, without the use of internal data packet, the packet may be used to import anonymous. Specific format is as follows:

Import _ "route package"

Anonymous imported package into packages with other means as will be compiled into an executable file.

init () function initializes

init () function describes

Importing package statement at the Go language program execution automatically trigger pack internal init()call functions. Note that:  init()takes no parameters and returns no value. init()It is called automatically executed when the program runs, you can not take the initiative to call it in your code.

Packet initialization sequence is performed as shown below:Package init () execution timing

init () function execution order

Go language from the package will mainbegin checking all packages import of packets, each packet may be imported and other packages. Go compiler thereby constructing a tree-like packet reference relationship, and then decide to compile a reference sequence according to sequence, the compiled code of the packets sequentially.

At runtime, the final package will be introduced first and calls its initialization init()functions, the following illustration:

init between packages () execution order

Guess you like

Origin www.cnblogs.com/waller/p/12024459.html