Govendor is another dependency package management tool of Go. Its appearance can avoid the problem that different users obtain different dependency library versions from the outside after clone the same project, which makes up the defect of GOPATH package management well. Govendor adds the dependency packages required by the project to the vendor directory of the project, and the vendor.json file in the directory saves information such as the path of the added dependency packages.

(For the sake of the integrity of the body of knowledge and a thorough understanding of the various package management tools, this article focuses only on the existence of Govendor and does not recommend its use.)

1. Overview of Govendor

Go engineering based packages often Go a get command is used to obtain, for example: Go get github.com/spf13/cobra/cobra, will depend on the path of the download package to GOPATH.

As Go has been upgraded, after Go 1.5, Go provides the GO15VENDOREXPERIMENT environment variable (which is enabled by default in Go 1.6) and the Govendor package management tool, It is used to adjust the application path search during go build to the current project /vendor directory, effectively solving the problem that different projects use their own independent dependent package directory.

1.1 introduce Govendor

Govendor is a package management tool introduced after Go 1.5.

The basic idea is to place the source code of the referenced external package under the vendor directory of the current project (similar to node_modules directory of Nodejs). After Go 1.5, the Go code will first look for the dependent package from the vendor directory. If the vendor directory is not found, I’m going to look in GOPATH, I’m not going to find anything and I’m going to look in GOROOT.

Features:

  • Can be usedgovendor add/update$GOPATHCopy existing dependencies.
  • If I were to ignorevendor/*/Can be used.govendorSynchronously recover dependencies.
  • Straight throughgovendor fetchThe controller adds new dependencies or updates existing dependencies.
  • Can be usedgovendor migrate Implement inter-system migration.
  • supportLinux.OS X.WindowsOr even all existing operating systems.
  • supportGit.Hg.SVN.BZRA path must be specified.

1.2 Problems solved

  • Different version dependency problem: solve different users incloneThe problem of obtaining different dependency library versions from the outside after the same project.
  • Migration problem: Copy the Go source code to the current projectvendorDirectory, thus packaging the current project code to any machine$GOPATH/srcThe following can be compiled to avoid excessive external dependencies of project code. After migration, there is no need to do it againgo getPull external dependency packages, againgo getThe version of the external dependency package pulled back may not be the same as that used during project development, which can cause compilation errors.

1.3 Common Commands

Govendor provides many commands for you to use. The following lists some common commands. You can view more commands through govendor –help.

The command function
init Initialize thevendordirectory
list List all dependency packages
add Add a package tovendorDirectory, such asgovendor add +externalAdd all external packages
update from$GOPATHUpdate dependency packages tovendordirectory
remove fromvendorDelete dependencies from management
status Lists all missing, expired, and modified packages
fetch Add or update packages locallyvendordirectory
sync There are localvendor.jsonWhen to pull the dependency package, match the version recorded
get similargo getDirectory to pull dependent packages tovendordirectory

2. Quick start

2.1 installation

Use the go get command to quickly install:

go get -u -v github.com/kardianos/govendor
Copy the code

You are advised to add $GOPATH/bin to the PATH to use the govendor command.

2.2 the initialization

Enter the root directory of the project and run the govendor init command to automatically create a vendor directory in the root directory of the project and generate vendor.json(store the version information of the package).

govendor init
Copy the code

2.3 pull the package

Use the govendor fetch or govendor get command to pull external dependency packages remotely. Such as:

govendor fetch github.com/spf13/cobra/cobra
Copy the code

3, summary

This article mainly explains the existence of Govendor and how to use it, but as Go grows, it is replaced by Go Modules, so it is not recommended to use Govendor. The next article will expand on Go Modules, which are currently the preferred and required package management tools.