Home > Article > Backend Development > What should I do if golang cannot import packages?
Solution to the problem that golang cannot import packages:
Import packages:
Standard package use is a given short path, such as "fmt", "net/http"
For your own package, you need to specify a directory under the working directory (GOPATH). The import package is actually based on the working directory. Folder directory
Multiple ways to import packages:
Import directly from the $GOPATH/src
directoryimport "test/lib"
(The path is actually $GOPATH/src/test/lib)
Alias import: import alias_name "test/lib"
, when used in this way, you can directly use the alias
Use the dot to import: import. "test/lib"
, function When using it, simply omit the package name
and use underscores to import: import _ "test/lib". This operation actually just introduces the package. When a package is imported, all its init() functions will be executed, but sometimes you don't really need to use these packages, you just want its init() function to be executed. At this time, you can use the _ operation to reference the package. Even if you use the _ operation to reference a package, you cannot call the exported functions in the package through the package name, but just to simply call its init function ().
Often these init functions register the engines in their own packages so that they can be easily used by the outside world. For example, packages that implement database/sql all call sql.Register(name) in the init functions. string, driver driver.Driver)
Register yourself, and then it can be used externally.
Relative path importimport "./model"//
The model directory in the same directory as the current file, but this method of import is not recommended
Package import Process description:
The initialization and execution of the program start from the main package. If the main package also imports other packages, they will be imported in sequence during compilation. Sometimes a package will be imported by multiple packages at the same time, so it will only be imported once (for example, many packages may use the fmt package, but it will only be imported once, because there is no need to import it multiple times).
When a package is imported, if the package also imports other packages, the other packages will be imported first, and then the package-level constants and variables in these packages will be initialized, and then init will be executed. function (if any), and so on. After all imported packages are loaded, the package-level constants and variables in the main package will be initialized, then the init function in the main package will be executed (if it exists), and finally the main function will be executed. The following figure explains the entire execution process in detail:
Note:
There are several ways to import Go packages, with different uses. The code is stored uniformly in the working directory. There will be many packages in the working directory. Different packages are organized by directory, and the packages are composed of multiple code files. When importing a package, import it according to the unique path of the package. The imported package must be used by default. If it is not used, the compilation will fail and needs to be removed to reduce the introduction of unnecessary code. Of course, there are other usage scenarios. By default, we use the file name as the package name for easy understanding. Different packages organize different function implementations to facilitate understanding.
Did you use the package source code or .a when compiling?
A non-main package will generate a .a
file after compilation (generated in a temporary directory, unless it is installed to $GOROOT
or # using go install ##$GOPATH, otherwise you won’t be able to see it. a), used for subsequent executable program linking. For example, the source code path corresponding to the package in the Go standard library is:
$GOROOT/src, and the path to the compiled .a file of the package in the standard library is
$GOROOT/pkg/darwin_amd64 Down. A strange question arises in my head. When compiling, does the compiler use .a or source code?
.a file in the temporary directory compiled with the latest source code.
In Go language, is the last element in the path after import the package name or the path name?
import m "lib/math" m refers to the only package in the lib/math path. If the compiler finds two packages in this path, it is not allowed. Compile Error reporting
Recommended tutorial: "go language tutorial"
The above is the detailed content of What should I do if golang cannot import packages?. For more information, please follow other related articles on the PHP Chinese website!