Defragmentation of language packs go

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

Package Introduction

Package (package) is more than a collection of Go source code, is an advanced code reuse program, go language provides us with a lot of built-in packages, such as fmt, os, io and so on.

Custom packages

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

package 包名

Note:
1, the next folder can have only one package, a package of the same file can not be in more than one folder.
2, the package name and the name of the folder may not be the same as the package name can not contain - symbol
3, package package package called main entrance of the application, will not get an executable file when the source code is not included in the main package compile time .

Visibility

If you want to reference a packet in a different package identifier (e.g., variables, constants, types, functions, etc.), the identifier must be visible outside (public). Capitalize the first letter in the language just to go identifiers can make the external visible identifiers.
For example, we define a package called pkg2 package, as follows:

var a = 100

const Mode = 1

type person struct {
    name string
}

func Add(x, y int) int {
    return x + y
}
func age() {
    var Age = 18
    fmt.Println(Age)
}

Struct field and method names interface if the first letter is capitalized, external package can access the fields and methods, such as:

type Student struct {
    Name string
    class string
}
type Payer interface{
    init()
    Pay()
}

Introducing the package

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

import "包的路径"

Precautions:

  • import import statement usually at the beginning of the file package declaration statements below.
  • Introducing the package name use double quotation wrapped.
  • Packages are from $ GOPATH / src / after counted using / split a route.
  • Go language prohibiting circulation import package.

Custom package name

When importing the package name, package name we can also import settings alias. The following format:

import 别名  "包的路径"

Multi-line import define an alias:

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 _  "包的路径"

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

init () function initializes

init () function describes

When the Go language program to perform the import package statements will automatically trigger a call to the function package inside init (), it should be noted that: init () function has no parameters and returns no value. init () function at runtime is invoked automatically performed, it can not take the initiative to call the code.
Packet initialization sequence is performed as shown below:

Defragmentation of language packs go

init () function execution order

Go language pack will begin checking all packages from the main pack import of each package and other packages may be imported, thereby constructing the Go compiler package references a tree-like relationship, then decided to compile a reference sequence according to the order, followed by these packages compiled code.
At runtime, the final package will be introduced first and calls its initialization () function, as shown init:
Defragmentation of language packs go

Guess you like

Origin blog.51cto.com/13766835/2416868