Table of Contents
vendor features
vendor usage recommendations
govendor Introduction
govendor Requirements for using
Installation
Initialization
Common commands
govendor subcommands
Status
Home Common Problem A brief discussion on govendor, the Go package dependency management tool

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

Apr 29, 2019 pm 01:45 PM
golang Package management

govendor is a Go package dependency management command line tool based on the vendor mechanism. 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 variable GO15VENDOREXPERIMENT=1 needs to be set manually.

When executing the go build or go run command, 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 the go env command.

When version 1.7 was released, this environment variable was removed and the vendor feature was enabled by default.

vendor usage recommendations

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

    Related tutorials: go video tutorial

govendor Introduction

govendor is a directory based on vendor Mechanical package management tool.

  • Supports analyzing dependent packages from the project source code and copying them from $GOPATH to the vendor directory of the project
  • Supported Specify the version of the package, and use vendor/vendor.json for package and version management, which is similar to PHP's Composer
  • supports using govendor add/ The update command copies the dependent package from $GOPATH
  • If the vendor/*/ file is ignored, you can use govendor sync to restore the dependency Package
  • can be directly used govendor fetch to add or update dependent packages
  • available govendor migrate from other vendor package management tools One-click migration to govendor
  • 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/src directory
  • If the Go version is 1.5, you must manually set the environment variable set GO15VENDOREXPERIMENT=1

Installation

go get -u github.com/kardianos/govendor
Copy after login

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

export PATH="$GOPATH/bin:$PATH"
Copy after login

Initialization

Execute the following command in the project root directory for vendor Initialization:

govendor init
Copy after login

In the project root directory, that is The vendor directory and vendor.json file will be automatically generated. At this time, the content of the vendor.json file is:

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

Common commands

  • will be referenced and under $GOPATH Copy the package to the vendor directory
govendor add +external
Copy after login
  • Copy only the specified package from $GOPATH
govendor add gopkg.in/yaml.v2
Copy after login
  • List all referenced packages in the code and their status
govendor list
Copy after login
 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
Copy after login
  • List which packages a package is referenced
govendor list -v fmt
Copy after login
 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
Copy after login
  • From the remote Add or update a package to the warehouse ( will not also save a copy in $GOPATH)
govendor fetch golang.org/x/net/context
Copy after login
  • 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".
Copy after login
  • Only format the project's own code (no changes in the vendor directory)
govendor fmt +local
Copy after login
  • Only build and compile the internal code of the project Package
govendor install +local
Copy after login
  • Only test test cases inside the project
govendor test +local
Copy after login
  • Build allvendor packages
govendor install +vendor,^program
Copy after login
  • Pull all dependent packages to the vendor directory (including $GOPATH existing or non-existing packages)
govendor fetch +out
Copy after login
  • The package is already in the vendor directory, but I want to update it from $GOPATH
govendor update +vendor
Copy after login
  • has been modified $GOPATH Now I want to update the modified and uncommitted package to vendor
govendor update -uncommitted <updated-package-import-path>
Copy after login
  • 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
Copy after login

will now pull the code from myfork instead of normal.

  • vendor.json records the dependency package information, how to pull updates
govendor sync
Copy after login

govendor subcommands

each For detailed usage of subcommands, you can check how the source code package is implemented through govendor COMMAND -h or read github.com/kardianos/govendor/context.

##initCreatelistList & filter dependent packages and their statusaddCopy the package from updateUpdate dependency packages to project removefrom statusList all missing, expired and modified packages fetchAdd or update packages from the remote repository to the project syncPull 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 LICENSEshell of the package can run multiple govendor commands at one time
Subcommand Function
vendor directory and vendor.json file
$GOPATH to the project vendor directory
vendor from $GOPATH Directory
vendor Directory to remove dependent packages
vendor directory (will not be stored in $GOPATH)
vendor directory based on vendor.json#migrate
get
$GOPATHvendor directorylicense
govendor Status Parameter

Status

AbbreviationMeaning locallLocal package, that is, the package written inside the project externale The external package is in but not in the project directory vPackages already in the directorysPackages in the standard library excludedxExplicitly excluded external packages unuseduUnused packages, that is, in the directory, but not referenced in the projectPackage that is referenced but not foundThe main program package can be compiled into an executable file Package is equivalent to the status of All packagesThe subcommands that support status parameters are: 、add、
GOPATHvendor vendor
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 of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to safely read and write files using Golang? How to safely read and write files using Golang? Jun 06, 2024 pm 05:14 PM

Reading and writing files safely in Go is crucial. Guidelines include: Checking file permissions Closing files using defer Validating file paths Using context timeouts Following these guidelines ensures the security of your data and the robustness of your application.

How to configure connection pool for Golang database connection? How to configure connection pool for Golang database connection? Jun 06, 2024 am 11:21 AM

How to configure connection pooling for Go database connections? Use the DB type in the database/sql package to create a database connection; set MaxOpenConns to control the maximum number of concurrent connections; set MaxIdleConns to set the maximum number of idle connections; set ConnMaxLifetime to control the maximum life cycle of the connection.

How to save JSON data to database in Golang? How to save JSON data to database in Golang? Jun 06, 2024 am 11:24 AM

JSON data can be saved into a MySQL database by using the gjson library or the json.Unmarshal function. The gjson library provides convenience methods to parse JSON fields, and the json.Unmarshal function requires a target type pointer to unmarshal JSON data. Both methods require preparing SQL statements and performing insert operations to persist the data into the database.

Golang framework vs. Go framework: Comparison of internal architecture and external features Golang framework vs. Go framework: Comparison of internal architecture and external features Jun 06, 2024 pm 12:37 PM

The difference between the GoLang framework and the Go framework is reflected in the internal architecture and external features. The GoLang framework is based on the Go standard library and extends its functionality, while the Go framework consists of independent libraries to achieve specific purposes. The GoLang framework is more flexible and the Go framework is easier to use. The GoLang framework has a slight advantage in performance, and the Go framework is more scalable. Case: gin-gonic (Go framework) is used to build REST API, while Echo (GoLang framework) is used to build web applications.

Transforming from front-end to back-end development, is it more promising to learn Java or Golang? Transforming from front-end to back-end development, is it more promising to learn Java or Golang? Apr 02, 2025 am 09:12 AM

Backend learning path: The exploration journey from front-end to back-end As a back-end beginner who transforms from front-end development, you already have the foundation of nodejs,...

How to find the first substring matched by a Golang regular expression? How to find the first substring matched by a Golang regular expression? Jun 06, 2024 am 10:51 AM

The FindStringSubmatch function finds the first substring matched by a regular expression: the function returns a slice containing the matching substring, with the first element being the entire matched string and subsequent elements being individual substrings. Code example: regexp.FindStringSubmatch(text,pattern) returns a slice of matching substrings. Practical case: It can be used to match the domain name in the email address, for example: email:="user@example.com", pattern:=@([^\s]+)$ to get the domain name match[1].

Golang framework development practical tutorial: FAQs Golang framework development practical tutorial: FAQs Jun 06, 2024 am 11:02 AM

Go framework development FAQ: Framework selection: Depends on application requirements and developer preferences, such as Gin (API), Echo (extensible), Beego (ORM), Iris (performance). Installation and use: Use the gomod command to install, import the framework and use it. Database interaction: Use ORM libraries, such as gorm, to establish database connections and operations. Authentication and authorization: Use session management and authentication middleware such as gin-contrib/sessions. Practical case: Use the Gin framework to build a simple blog API that provides POST, GET and other functions.

How to use predefined time zone with Golang? How to use predefined time zone with Golang? Jun 06, 2024 pm 01:02 PM

Using predefined time zones in Go includes the following steps: Import the "time" package. Load a specific time zone through the LoadLocation function. Use the loaded time zone in operations such as creating Time objects, parsing time strings, and performing date and time conversions. Compare dates using different time zones to illustrate the application of the predefined time zone feature.