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/testcase
Project, 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/ssh
and/pkg/sftp
After 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/ssh
and/pkg/sftp
Store 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/testcase
The 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:
- from
GOPATH/src//test/testcase/testpackage/main/
Looking for the vendor directory below, it was not found, continue to search from the upper layer. - from
GOPATH/src//test/testcase/testpackage/
Looking for the vendor directory below, it was not found, continue to search from the upper layer. - from
GOPATH/src//test/testcase/
Look for the vendor directory, find the dependency package from the vendor directory, and end the search.
ifGOPATH/src//test/testcase/
Nextvendor
If there is no dependency package in the directory, the GOPATH directory will be returned to continue searching. This is the GOPATH mechanism.
In factvendor
Directories can exist in any directory of the project. But not recommended, ifvendor
The directory is too scattered, and the same dependency package may be in multiple projects.vendor
Appears 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
vendor
It solves the isolation problem between multiple projects well, but there are some shortcomings:
- The project dependencies are not clear and cannot be clearly seen
vendor
The 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.
vendor
It 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!