Home>Article>Backend Development> Some problems with package import in go language
import
GoUse packages as the basic unit to organize source code, all syntax visibility is Defined at the package level. Under the same package, there can be many different files, as long as each file belongs to the same package name.
The first line of each source code file must define which package it belongs to through the following syntax,
package xxx
Then it is the standard package or third-party package used to import this source code file, that is,
import ( "a/b/c" "fmt" )
The standard library will be searched from the GO installation directory, and the third-party library will be searched from the$GOPATH
defined by the developer. When neither is found, the compiler will report an error. When using third-party packages, when the source code and .a are both installed, the compiler links to the source code.
Note: The last c in a/b/c in the above statement is the directory name, not the package name.
When calling a method in a file, use the following format:
package.Methodxxx()
The package of multiple files in the same folder is generally defined as the name of the folder, but there are exceptions. For example, in the chestnut above, the package of all files under the c file is defined as fux. Then when calling the method of the files under this folder, you can only usefux.Methodxxx()
instead ofc..Methodxxx()
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 .a), which is used for subsequent executable program linking.
vendor
Go added vendor support for package management in version 1.5. Version 1.5 needs to setGO15VENDOREXPERIMENT="1"
to support this feature, and version 1.6 uses it as the default parameter configuration. The following rules for package import paths containing vendor directories are roughly as follows.
├── d ├── mypkg | └── main.go └── vendor └── q ├── q.go
When the above directory structure is imported q in main.go, it will first be searched from the vendor directory. If it cannot be found, it will be searched from the $GOPATH directory. If it cannot be found again, the compiler An error was reported.
For more go language knowledge, please pay attention to thego language tutorialcolumn.
The above is the detailed content of Some problems with package import in go language. For more information, please follow other related articles on the PHP Chinese website!