Package Introduction
Package ( Package Penalty for ) is 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 storage .go file folder. The folder following documents must all go add the following code in the first line of code, stating that the file belongs package.
package package name
Precautions:
- Folder contains a file directly below can be attributed to a package , a same package file can not be in multiple folders.
- Package name and the name of the folder may not be the same as the package name can not contain - symbol.
- Package called main packet is a packet entry application, after which the package will be a compiled executable file, the compiler does not contain a main source of the packet will not be executable.
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:
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 = MODE . 1 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 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 //Local function variables, not visible outside the package, only within the current function 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 type struct { 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, use the import 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 / after counted using / path for the partition.
- 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 defined aliases
Introducing single-line manner to define an alias:
import "fmt" import m "github.com/gbb/studygo/pkg_test" func main() { fmt.Println(m.Add(100, 200)) fmt.Println(m.Mode) }
Introducing a plurality of rows define aliases
Multi-line import define an alias:
import ( "fmt" m "github.com/gbb/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 _ " package Path "
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 packet inside init () function is called. Note that: the init () function has no parameters and returns no value. init () function is invoked program runs automatically performed, it can not take the initiative to call the code.
Packet initialization sequence is performed as shown below:
init () function execution order
Go language packs will be from the main start 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 () the init function, shown below: