Home >Backend Development >Golang >Introduction to three ways to introduce external dependencies in go
import "github.com/astaxie/beego" Compilation error solution:
go get
Make sure your GOPATH is the project directory, the code is in the src directory, and then enter: go get github.com/astaxie/beego in the command prompt, and then the external package to be introduced is generated under the local src.
Note: When using the GoLand tool, configure settings->Go->GOPATH->Project GOPATH to the current project directory
go module
Go Module can only be used with Go version 1.11 or above. Go Module is turned off by default below version 1.13.
First you need to set the environment variable set GO111MODULE=on, create a new project folder, enter the new path and execute go mod init. Generate the go.mod file in the folder, then place the go file that needs to introduce the external package in the project directory, compile the file, and the external package will be downloaded to the local GOPATH/pkg/mod directory
Note: When using the GoLand tool, do not configure Project GOPATH as the current project directory. It is best not to configure Project GOPATH, but configure Module GOPATH
vendor directory
First Install govendor: go get -u -v github.com/kardianos/govendor. After downloading, configure the environment variable GOPATH/bin and type the command
govendor -version to check whether the installation is successful.
In the directory of GOPATH/src, create a new project folder, enter the new path and execute gogovendor init, vendor/vendor.json will be generated in the folder.
Then place the go file that needs to introduce the external package in the project directory, use the command govendor fetch github.com/golang/glog to download the external file to the local vendor/, and add this in vendor.json Dependent package information, where govendor fetch is to add dependent packages from the remote library, and to add dependent packages from $GOPATH, use govendor add
For more go language knowledge, please pay attention to php Chinese websitego Language tutorial column.
The above is the detailed content of Introduction to three ways to introduce external dependencies in go. For more information, please follow other related articles on the PHP Chinese website!