A, Go language package (package)
Introduction 1.1 Package
包(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
, io
and so on.
1.2 Definitions package
We can also create your own package according to their needs. A package can be simply interpreted as a stored .go
document 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 home
package
, a samepackage
file 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
main
bag of entry packet of the application, after which the package will be a compiled executable file, the compiler does not containmain
the source package you will not get an executable file.
1.3 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 pkg2
bag, as follows:
package pkg2
import "fmt"
// 包变量可见性
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 these fields and methods. E.g:
type Student struct {
Name string //可在包外访问的方法
class string //仅限包内访问的字段
}
type Payer interface {
init() //仅限包内访问的方法
Pay() //可在包外访问的方法
}
1.4 introduction package
To refer to the contents of other packages in your code, you use import
the keyword into the use of the package. Specific syntax is as follows:
import "包的路径"
Precautions:
- import import statements are usually placed at the beginning of the following package declaration.
- Introducing the package name use double quotation wrapped.
- Package name from
$GOPATH/src/
the start of the calculation, using/
a route separated. - Go language prohibiting circulation import package.
Introducing single-line 1.4.1
Introducing single-line format is as follows:
import "包1"
import "包2"
1.4.2 multi-line import
Import multi-line format is as follows:
import (
"包1"
"包2"
)
1.5 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 别名 "包的路径"
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)
}
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)
}
1.6 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.
1.7 init () function to initialize
1.7.1 init () function description
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:
1.7.2 init () function execution order
Go language from the package will main
begin 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: