SoFunction
Updated on 2025-03-03

Use of Vendo mechanism in Go

1. Introduction

Since Go 1.6, the vendor mechanism has been officially enabled. It allows project dependencies to be placed in a vendor directory located in this project. This vendor directory can be simply understood as a private GOPATH directory. When the project is compiled, the compiler will prioritize looking for dependency packages from vendors, and if they cannot be found in vendors, they will then search for them in GOPATH.

2. vendor directory location

A project can have multiple vendor directories, which are located at different directory levels. It is recommended that each project only place one vendor directory in the root directory.
If there is one/test/testcaseProject, project directory structure is as follows:

GOPATH/src//test/testcase/testpackage/main/

inThe following packages are dependent on:

import (
    "/x/crypto/ssh"
    "/pkg/sftp"
)

If you want to compile this project without using the vendor directory, the GOPATH directory should go as follows:

GOPATH/src//test/testcase/
GOPATH/src//pkg/sftp
GOPATH/src//x/crypto/ssh

That is, all dependencies are located under GOPATH/src.
In order to use/x/crypto/sshand/pkg/sftpAfter the version is solidified, the vendor mechanism can be used.
In the project/test/testcase/Create a vendor directory in the root directory and put/x/crypto/sshand/pkg/sftpStore in this directory and make it part of the project:

GOPATH/src//test/testcase/testpackage/main/
GOPATH/src//test/testcase/vendor//pkg/sftp/
GOPATH/src//test/testcase/vendor//x/crypto/ssh/

The benefit of using vendor is in the project/test/testcaseThe software it depends on can be released together when publishing. The compile time will not be affected by the GOPATH directory, even if there is one under GOPATH
Dependency package with the same name but different versions.

3. Search order

The compiler will search up step by step from the directory where the source code file is located. In the example above, it is compiled.When the compiler searches for dependency packages is:

  • fromGOPATH/src//test/testcase/testpackage/main/Looking for the vendor directory below, it was not found, continue to search from the upper layer.
  • fromGOPATH/src//test/testcase/testpackage/Looking for the vendor directory below, it was not found, continue to search from the upper layer.
  • fromGOPATH/src//test/testcase/Look for the vendor directory, find the dependency package from the vendor directory, and end the search.

ifGOPATH/src//test/testcase/NextvendorIf there is no dependency package in the directory, the GOPATH directory will be returned to continue searching. This is the GOPATH mechanism.
In factvendorDirectories can exist in any directory of the project. But not recommended, ifvendorThe directory is too scattered, and the same dependency package may be in multiple projects.vendorAppears in
Many times, this dependent package will be compiled into the binary file multiple times, resulting in a sharp increase in the size of the binary file. It is also very likely that multiple versions of the same dependent package will be used in a project.

4. The shortcomings of vendor

vendorIt solves the isolation problem between multiple projects well, but there are some shortcomings:

  • The project dependencies are not clear and cannot be clearly seenvendorThe version of the dependency package in the directory.
  • It is inconvenient to review when upgrading dependency packages.

What is more serious is the problem of the sharp increase in the size of the binary file. For example, the project relies on open source packages A and B, but there is also a vendor directory in A, and B is also placed, so two open source packages B will appear in the project.
If the versions of the two open source package B are inconsistent or incompatible, the consequences are terrible.

vendorIt can solve the problems in most projects, but there are also many shortcomings.

This is the end of this article about the use of the Vendo mechanism in Go. For more related Go Vendo content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!