Go > Packages and Modules > Go Modules > Managing dependencies (go get, go mod tidy)
Go Modules: Vendoring Dependencies
This example demonstrates how to use Go modules to vendor dependencies in a Go project. Vendoring copies the project's dependencies into a local vendor
directory within the project, ensuring that the project can be built even if the original dependencies are unavailable.
Understanding Vendoring
Vendoring is the process of copying the project's dependencies into a local directory (usually named vendor
) within the project. This allows the project to be built even if the original dependencies are unavailable (e.g., if a remote repository is down or has been removed).
Enabling Vendoring
The go mod vendor
command copies all dependencies listed in the go.mod
file into a vendor
directory in the project root.
go mod vendor
Using Vendored Dependencies
After running go mod vendor
, Go will automatically use the dependencies in the vendor
directory. No code changes are necessary.
Commiting the Vendor Directory
It's common practice to commit the vendor
directory to your version control system. This ensures that everyone working on the project uses the same versions of the dependencies, regardless of their network connectivity or the availability of the remote repositories.
Real-Life Use Case
Imagine you're working on a critical project that needs to be built and deployed reliably, even in environments with limited or no internet access. Vendoring ensures that the project can be built using the local copies of the dependencies, without relying on external sources.
Best Practices
go mod vendor
after adding, updating, or removing dependencies.vendor
directory to version control.
When to Use Vendoring
Use vendoring when you need to ensure that your project can be built reliably, regardless of the availability of external dependencies. This is particularly important for critical projects or projects deployed in environments with limited network access.
Pros of Vendoring
Cons of Vendoring
vendor
directory can significantly increase the size of the repository.
FAQ
-
What if I don't want to commit the
vendor
directory?
While it's common to commit thevendor
directory, you can choose not to. However, this means that anyone building the project will need to have access to the internet to download the dependencies. -
How do I update vendored dependencies?
Update the dependencies usinggo get
, then rungo mod vendor
again to update thevendor
directory. -
Does vendoring affect build performance?
Vendoring can slightly improve build performance by reducing the need to download dependencies from remote repositories.