A package is a collection of multiple Go source codes and is an advanced code reuse solution. Go language packages use the organizational form of a directory tree. Generally, the name of a package is the name of the directory where its source file is located. Packages can be defined in very deep directories. The definition of the package name does not include the directory path, but the package is referenced. Generally use full path reference.
The operating environment of this tutorial: Windows 7 system, GO version 1.18, Dell G3 computer.
The Go language uses packages to organize source code. A package is a collection of multiple Go source codes and is an advanced code reuse solution. Go language provides us with many built-in packages, such as fmt, os, io, etc.
Packages and folders in Golang have a one-to-one correspondence and must be created in the GOPATH directory before they can be used. If a package in Golang needs to reference the contents of another package, it must be imported using the import keyword before it can be used.
Any source code file must belong to a certain package, and the first line of valid code in the source code file must be the package pacakgeName
statement, through which you declare the package you are in.
Basic concepts of packages
Go language packages use the organization form of a directory tree. Generally, the name of a package is the directory where its source file is located. Although the Go language does not mandate that the package name must have the same name as the directory name where it is located, it is still recommended that the package name have the same name as the directory where it is located, so that the structure is clearer.
Packages can be defined in very deep directories. The definition of the package name does not include the directory path, but the full path is generally used when referencing the package. For example, define a package c under GOPATH/src/a/b/
. In the source code of package c, you only need to declare it as package c
instead of package a/b/c
. However, when importing the c package, you need to bring the path, such as import "a/b/c"
.
Idiomatic usage of packages:
Package names are generally lowercase, use a short and meaningful name.
The package name generally has the same name as the directory where it is located, or it can be different. The package name cannot contain special symbols such as -.
Packages generally use the domain name as the directory name, which ensures the uniqueness of the package name. For example, the packages of GitHub projects are generally placed in GOPATH/src/github.com/userName /projectName
directory.
The package named main is the entry package of the application. When compiling the source code file that does not contain the main package, you will not get an executable file.
All source code files in a folder can only belong to the same package. Source code files that also belong to the same package cannot be placed in multiple folders.
Package import
To reference the contents of other packages in the code, you need to use the import keyword to import. package. The specific syntax is as follows:
import "包的路径"
Notes:
The import statement is usually placed below the package declaration statement at the beginning of the source code file;
The imported package name needs to be wrapped in double quotes;
The package name is calculated starting from GOPATH/src/
, and uses / to separate the paths. .
Package import path
There are two ways to write the package reference path, namely full path import and relative Path import.
Full path import
The absolute path of the package is the storage path of the package behind GOROOT/src/ or GOPATH/src/, as shown below:
import "lab/test" import "database/sql/driver" import "database/sql"
The meaning of the above code is as follows:
The test package is a customized package, and its source code is located in the GOPATH/src/lab/test directory;
The source code of the driver package is located in the GOROOT/src/database/sql/driver directory;
The source code of the sql package is located in the GOROOT/src/database/sql directory.
Relative path import
Relative paths can only be used to import packages under GOPATH, and standard packages can only be imported using the full path.
For example, the path of package a is GOPATH/src/lab/a, and the path of package b is GOPATH/src/lab/b. If you import package a in package b, you can use the relative path to import it. Way. An example is as follows:
// 相对路径导入 import "../a"
Of course, you can also use the full path to import above, as shown below:
// 全路径导入 import "lab/a"
Package loading
Through the previous series of studies, I believe everyone has a general understanding of the startup and loading process of the Go program. Before executing the main function of the main package, the Go boot program will first initialize the entire program package. The entire execution process is shown in the figure below.
Figure: Initialization of Go package
The initialization of Go language package has the following characteristics:
The package initialization program starts from the package referenced by the main function, searches for package references step by step, until it finds a package that does not reference other packages, and finally generates a directed acyclic graph of package references.
The Go compiler will convert the directed acyclic graph into a tree, and then initialize the package layer by layer starting from the leaf nodes of the tree.
The initialization process of a single package is as shown in the figure above. Constants are initialized first, then global variables, and finally the init function of the package is executed.
Golang package usage summary
Go language source code organization uses the form of packages. The main function of Go language can only be executed by the system in the main package.
【Related recommendations: Go video tutorial, Programming teaching】
The above is the detailed content of What is a package in Go language. For more information, please follow other related articles on the PHP Chinese website!