Home>Article> A brief discussion on govendor, the Go package dependency management tool

A brief discussion on govendor, the Go package dependency management tool

little bottle
little bottle forward
2019-04-29 13:45:28 3706browse
govendor is a Go package dependency management command line tool based on the vendormechanism. Non-intrusive integration with native vendors, and also supports migration from other dependency management tools. It can easily realize the development and management of different versions of the same package in different projects without mutual intrusion.

vendor features

At the beginning, Go did not provide a more appropriate package management tool. The vendor feature is provided starting from version 1.5, but the environment variableGO15VENDOREXPERIMENT=1needs to be set manually.

When executing thego buildorgo runcommand, the package will be searched in the following order:

  • The vendor under the current package Directory
  • Search in the upper directory until you find the vendor directory under src
  • Search in the GOROOT directory
  • Look for dependent packages under GOPATH

When version 1.6 was released, the value of this environment variable was set to 1 by default. This value can be viewed using thego envcommand.

When version 1.7 was released, this environment variable was removed and thevendorfeature was enabled by default.

vendor usage recommendations

  • A library project (packagethat does not containmain) should not store external files in its own version control The packages are in thevendordirectory, unless there is a special reason and you know why you want to do this.
  • In an application, (packagecontainingmain), it is recommended that there is only onevendordirectory, and it is located in the code base level directory.

    Related tutorials:go video tutorial

govendor Introduction

govendor is a directory based onvendorMechanical package management tool.

  • Supports analyzing dependent packages from the project source code and copying them from$GOPATHto thevendordirectory of the project
  • Supported Specify the version of the package, and usevendor/vendor.jsonfor package and version management, which is similar to PHP'sComposer
  • supports usinggovendor add/ The updatecommand copies the dependent package from$GOPATH
  • If thevendor/*/file is ignored, you can usegovendor syncto restore the dependency Package
  • can be directly usedgovendor fetchto add or update dependent packages
  • availablegovendor migratefrom othervendorpackage management tools One-click migration togovendor
  • Supports Linux, macOS, Windows, and even all existing operating systems
  • Supports Git, Hg, SVN, BZR (a path must be specified )

govendor Requirements for using

:

  • The project must be in the$GOPATH/srcdirectory
  • If the Go version is 1.5, you must manually set the environment variableset GO15VENDOREXPERIMENT=1

Installation

go get -u github.com/kardianos/govendor

For convenience and quick usegovendor, it is recommended to add$GOPATH/binto PATH. Linux/macOS settings are as follows:

export PATH="$GOPATH/bin:$PATH"

Initialization

Execute the following command in the project root directory forvendorInitialization:

govendor init

In the project root directory, that is Thevendordirectory andvendor.jsonfile will be automatically generated. At this time, the content of thevendor.jsonfile is:

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

Common commands

  • will be referenced and under$GOPATHCopy the package to thevendordirectory
govendor add +external
  • Copy only the specified package from$GOPATH
govendor add gopkg.in/yaml.v2
  • List all referenced packages in the code and their status
govendor list
e github.com/gin-contrib/sse e github.com/gin-gonic/gin e github.com/gin-gonic/gin/binding e github.com/gin-gonic/gin/internal/json e github.com/gin-gonic/gin/render e github.com/golang/protobuf/proto e github.com/mattn/go-isatty e github.com/ugorji/go/codec e gopkg.in/go-playground/validator.v8 e gopkg.in/yaml.v2 pl govendor-example m github.com/json-iterator/go m golang.org/x/sys/unix
  • List which packages a package is referenced
govendor list -v fmt
s fmt ├── e github.com/gin-contrib/sse ├── e github.com/gin-gonic/gin ├── e github.com/gin-gonic/gin/render ├── e github.com/golang/protobuf/proto ├── e github.com/ugorji/go/codec ├── e gopkg.in/go-playground/validator.v8 ├── e gopkg.in/yaml.v2 └── pl govendor-example
  • From the remote Add or update a package to the warehouse (will notalso save a copy in$GOPATH)
govendor fetch golang.org/x/net/context
  • Install 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".
  • Only format the project's own code (no changes in thevendordirectory)
govendor fmt +local
  • Only build and compile the internal code of the project Package
govendor install +local
  • Only test test cases inside the project
govendor test +local
  • Build allvendorpackages
govendor install +vendor,^program
  • Pull all dependent packages to thevendordirectory (including$GOPATHexisting or non-existing packages)
govendor fetch +out
  • The package is already in thevendordirectory, but I want to update it from$GOPATH
govendor update +vendor
  • has been modified$GOPATHNow I want to update the modified and uncommitted package tovendor
govendor update -uncommitted 
  • Fork a package, but it has not been merged yet. What should I do? Referring to the latest code package
govendor fetch github.com/normal/pkg::github.com/myfork/pkg

will now pull the code frommyforkinstead ofnormal.

  • vendor.jsonrecords the dependency package information, how to pull updates
govendor sync

govendor subcommands

each For detailed usage of subcommands, you can check how the source code package is implemented throughgovendor COMMAND -hor readgithub.com/kardianos/govendor/context.

##init Create list List & filter dependent packages and their status add Copy the package from update Update dependency packages to project remove from status List all missing, expired and modified packages fetch Add or update packages from the remote repository to the project sync Pull matching packages to the One-click migration from other package management tools based on vendor is similar to go get , Download the package to , and then copy the dependent package to the List all dependencies The LICENSE shell of the package can run multiple govendor commands at one time
Subcommand Function
vendordirectory andvendor.jsonfile
$GOPATHto the projectvendordirectory
vendorfrom$GOPATHDirectory
vendorDirectory to remove dependent packages
vendordirectory (will not be stored in$GOPATH)
vendordirectory based onvendor.json#migrate
get
$GOPATHvendordirectorylicense
govendor Status Parameter

Status

Abbreviation Meaning local l Local package, that is, the package written inside the project external e The external package is in but not in the project directory v Packages already in the directory s Packages in the standard library excluded x Explicitly excluded external packages unused u Unused packages, that is, in the directory, but not referenced in the project Package that is referenced but not found The main program package can be compiled into an executable file Package is equivalent to the status of All packages The subcommands that support status parameters are: 、 add 、
GOPATHvendorvendor
vendor std
vendor ## missingm
program p
outside
external missing
all

list
update

removefetchGo modulesPudaxiben What's interesting is that starting from Go version 1.11, the official has built in more powerful Go modules to unify the chaotic situation of Go package dependency management for many years (the dep tool previously launched by Go officials was almost stillborn), and will be released in 1.13 It is officially enabled by default in this version.It has been favored and strongly recommended by the community, and it is recommended that new projects use Go modules.

The above is the detailed content of A brief discussion on govendor, the Go package dependency management tool. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete
Previous article:What is bs architecture Next article:What is bs architecture