packages in the Go language
We use other languages, such as Java,python, to have the concept of similar packages, go is no exception, the core idea is grouping and modularity. The human brain is very difficult to control large and complex things, it can be used to divide and conquer the strategy to make it modular, and thus easier to manage. The following is a tree chart of net packages in the standard library
Net
├─http
│├─cgi
││└─testdata
│├─cookiejar
│├─fcgi
│├─httptest
│├─httptrace
│├─httputil
│├─internal
│├─pprof
│└─testdata
├─internal
│└─socktest
├─mail
├─rpc
│└─jsonrpc
├─smtp
├─testdata
├─textproto
└─url Library Package and main package
When a program's package is declared as main (and the file contains a main function), it means that the program is eventually compiled into an executable program. If the program is not declared as main, it can be compiled into the form of a library. Import of Packages
After importing the package through import, you can use the variables and functions in the package, the following source code, we define a Lib package, which defines the addition, subtraction, multiplication, except 4 methods, and then in another package to use.
The contents of the library are as follows:
Package LIB1
func Add (x, y int) int {
Z: = x + y
return z
}
func Sub (x, y int) int {
Z: = X-y
return z
}
func Mul (x, y int) int {
Z: = x * y
return z
}
func Div (x, y int) int {
Z: = X/y
return z
}
The contents of the main package are as follows:
Package main
Import (
"FMT"
"LIB1"
)
func main () {
x: = 8
y: = 2
A: = Lib1. ADD (x, y)
b: = lib1. Sub (x, y)
c: = Lib1. Mul (x, y)
D: = lib1. Div (x, y)
fmt. Println (a)
FMT. Println (b)
FMT. Println (c)
FMT. Println (d)
}
Output:
10
6
16
4
Note the exported method in the library package if you want to make the package visible outside, the first letter is capitalized. such as add,sub ... Otherwise, the method is not visible outside the package and can only be used in this package (variables have the same principle). The go language does not provide keywords such as C + + and Java private,public,protected, but also reflects the philosophy of Go language: simple and practical first. Package Name import conflict (named import)
When importing a package using import, we can refer to the package by the package name, such as the above lib1, but if there is another person to develop the package name is also LIB1 (this is the last part of the package), that is, the occurrence of the package naming conflict how to handle it. You can handle this issue with an alias in front of the import name, renaming the package names "FMT" and "LIB1" for "Fmta" and "Liba", respectively, as follows
Package main
Import (
Fmta "FMT"
Liba "LIB1"
)
func main () {
x: = 8
y: = 2
A: = Liba. ADD (x, y)
b: = Liba. Sub (x, y)
c: = Liba. Mul (x, y)
D: = Liba. Div (x, y)
Fmta. Println (a)
Fmta. Println (b)
Fmta. Println (c)
Fmta. Println (d)
}
initialization function of the package init ()
There may be multiple files in a package, and if we want to do some initialization on this package, how to do it. Go provides an init () function (like the main function) that has no parameters and no return value, which can be initialized in the INIT function. For example, there are 3 packages "LIB1", "Lib2" "M1" represent two library packages and a main package containing the main function, each package has a corresponding go file, the package directory structure is as follows:
├─LIB1 A.go
├─lib2 B.go
└─M1 M.go
The M.go file in the "M1" package will import "LIB1" and further "LIB1" will import "Lib2". And the INIT function is defined in LIB1 and LIB2. The source code is as follows:
Lib2 in A2.go package
lib2
import (
"FMT"
)
func init () {
fmt. Println ("Lib2 init1")
}
func init () {
fmt. Println ("Lib2 init2")
}
LIB1 in A1.go package
lib1
import (
"FMT"
_ "Lib2"
)
func init () {
fmt. Println ("Lib1 init1")
}
func init () {
fmt. Println ("Lib1 init2")
}
M1 in the M.go package
main
import (
"FMT"
_ "LIB1"
)
func main () {
fmt. Println ("main")
}
Output:
Lib2 init1
Lib2 Init2
LIB1 init1
LIB1 Init2
Main
It is visible that main executes, and first executes the init at the time of import of each package. and the order in which the INIT functions are executed is performed in the order of dependency. Go will analyze dependencies recursively, then start with the main package and ensure that there is no cyclic dependency. Main package found import "LIB1", Lib1 found and import "Lib2", so first execute LIB2 init function, then return to execute LIB1 in the init function, and finally return to the main function to execute.
There can be multiple files in each package, and there can be more than one init () function in each file, but the order of these init functions go is not guaranteed. Anonymous import of packages
A compilation error is reported because go does not use the imported package. Therefore, you can use the following methods. Use an underscore "_" to represent the package name. This will still execute the INIT function in the import package.
Package lib1
Import (
"FMT"
_ "Lib2"
)