Go Vendor profile

Govendor is a package management tool based on the vendor directory mechanism.

Initially, Go did not provide a proper package management tool. Vendor features are available starting with version 1.5, but the environment variable GO15VENDOREXPERIMENT=1 needs to be manually set. When the go build or go run command is executed, packages are found in the following order:

  1. Look for dependency packages in the current vendor directory (if there is one).

  2. If no vendor directory exists in the current directory, go to the directory at the next higher level to search for the vendor directory.

  3. Repeat step 2 until you reach the $GOPATH/ SRC directory to see if there are dependent packages in the vendor directory.

  4. If the dependent package is not found, continue the search in the $GOROOT directory;

  5. If none is found, continue the search in the $GOPATH/ SRC directory.

The value of this environment variable was set to 1 by default when version 1.6 was released and can be viewed using the go env command. When version 1.7 is released, this environment variable is removed and vendor is enabled by default.

Govendor function

  • Support to analyze the dependent packages from the project source and copy them from $GOPATH to the project vendor directory;

  • Support the specified version of the package, and use vendor/vendor.json for package and version management;

  • Support for copying dependencies from $GOPATH with the govendor add/update command.

  • If vendor/*/ files are ignored, use govendor sync to restore dependencies.

  • Dependencies can be added or updated directly with Govendor Fetch;

  • You can use GoVendor Migrate to migrate from other Vendor packages.

  • Support Linux, macOS, Windows, and even all existing operating systems;

  • Support Git, Hg, SVN, BZR (must specify a path);

Govendor Usage suggestions

  • A library project (a package that does not contain main) should not store external packages in the vendor directory in its own version control unless it has a specific reason and knows why.

  • In an application (the package containing main), it is recommended to have only one vendor directory, at the code base level.

Govendor command

The command instructions
init Create the vendor directory and vendor.json file
list Lists filter dependent packets and their status
add Copy the package from $GOPATH to the project vendor directory
update Update dependencies from $GOPATH to the project vendor directory
remove Removes the dependent packages from the vendor directory
status Lists all missing, expired, and modified packages
fetch Add or update packages to project vendor directory from remote repository (not stored to $GOPATH)
sync Pull matching packages to vendor directory according to vendor.json
migrate One-click migration from other Vendor-based package management tools
get Similar to Go Get, download the package to $GOPATH and copy the dependent package to the vendor directory
license List the licenses of all dependent packages
shell You can run multiple govendor commands at once
  • State parameter

  • The subcommands that support status parameters are list, add, update, remove, and fetch

state abbreviations meaning
+local I Local packages, that is, packages written inside the project
+external e External packages, that is, in GOPATH but not in the project vendor directory
+vendor v Package already in vendor directory
+std s Packages from the standard library
+excluded x External packages that are explicitly excluded
+unused u Unused packages that are in the vendor directory but not referenced in the project
+missing m Packages that are referenced but cannot be found
+program p A main package that can be compiled into an execution file
+outside The state is +external +missing
+all All packages

Govendor use

The installation

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

To facilitate the use of Govendor, you are advised to add $GOPATH/bin to the PATH. The Linux/macOS Settings are as follows:

export PATH="$GOPATH/bin:$PATH"
Copy the code

Initialize the

Run the following command in the project root directory to initialize vendor:

govendor init
Copy the code

The vendor directory and vendor.json files are automatically generated in the root directory of the project. The vendor.json file is as follows:

{
	"comment": "",
	"ignore": "test",
	"package": [],
	"rootPath": "govendor-example"
}
Copy the code

Common commands

  • Will have been referenced and in$GOPATHCopy all packages undervendordirectory
govendor add +external
Copy the code
  • Only from$GOPATHTo copy the specified package
govendor add gopkg.in/yaml.v2
Copy the code
  • Lists all packages referenced in the code and their status
govendor list
Copy the code
  • Lists which packages a package is referenced by
govendor list -v fmt
Copy the code
  • Add or update a package from a remote repository (Don’tin$GOPATHSave one too.)
govendor fetch golang.org/x/net/context
Copy the code
  • Installs the specified version of the package
govendor fetch golang.org/x/net/context@a4bbce9fcae005b22ae5443f6af064d80a6f5a55 govendor fetch golang.org/x/net/context@v1 # Get latest v1.*.* tag or branch. govendor fetch golang.org/x/net/context@=v1 # Get the tag  or branch named "v1".Copy the code
  • Format only the project code itself (vendorUnchanged under directory)
govendor fmt +local
Copy the code
  • Build only packages that are built within the build project
govendor install +local
Copy the code
  • Test only test cases within the project
govendor test +local
Copy the code
  • Build allvendor
govendor install +vendor,^program
Copy the code
  • Pull all dependent packages tovendorContents (including$GOPATHExisting or non-existent packages)
govendor fetch +out
Copy the code
  • Package has beenvendorDirectory, but want from$GOPATHupdate
govendor update +vendor
Copy the code
  • Has changed the$GOPATHNow wants to update modified and uncommitted packages tovendor
govendor update -uncommitted <updated-package-import-path>
Copy the code
  • How do I refer to the latest code package if I Fork a package but have not merged it
govendor fetch github.com/normal/pkg::github.com/myfork/pkg
Copy the code

Instead of normal, the code is pulled from Myfork.

  • vendor.jsonHow do I pull the update
govendor sync
Copy the code

reference

  • https://shockerli.net/post/go-package-manage-tool-govendor/

  • https://lucasfcosta.com/2017/02/07/Understanding-Go-Dependency-Management.html