The replace directive in go.mod tells the go command to use a different source for a module, such as a local folder or a fork, and vendoring copies all dependency source code into a vendor directory inside your repository. Together they let you patch a dependency, work offline or build without network access. The key phrase is go mod replace vendor: replace changes where code comes from, vendor changes where the build reads it.
Both are tools with trade-offs. This guide shows the syntax, the rules that surprise people, how to create and use a vendor folder and when each is the right choice.
A replace line maps a module path, optionally with a version, to a replacement.
There are two forms of the right-hand side:
./ or ../ (or an absolute path). The folder must contain its own go.mod, and you write no version.If the left side has no version, every version of that module is replaced. With a version, only that version is.
You can edit this with a command instead of by hand:
replace directives count. If a dependency has its own replace lines, the go command ignores them. Your users who import your module will not see your replacements either.../lib exists on your machine only, so a clean clone or CI job breaks. Do not commit a local-path replace in a library.example.com/lib; only the source behind it changes.module line, unless it is a fork, which keeps the original path in its own go.mod.require line.Typical uses: testing an unreleased fix, carrying a patched fork until upstream merges it, or pointing at an internal mirror. For several modules developed together, a go.work workspace is usually cleaner than a local replace.
Vendoring stores a copy of every package your build needs in a top-level vendor folder. Create it with:
The command writes the needed packages and a vendor/modules.txt file that records the module versions and which ones are explicit. Only packages actually imported are copied, not whole repositories, and test files of dependencies are left out.
If the go line in go.mod is 1.14 or higher and a vendor folder exists, the go command builds from vendor by default. You can also choose explicitly:
-mod=vendor reads only the vendor folder and needs no network or module cache. If vendor/modules.txt is out of sync with go.mod, the build fails with an inconsistent vendoring error; run go mod vendor again to fix it. Commit the vendor folder if you rely on it, and re-run the command after each dependency change. A check in CI such as go mod vendor followed by git diff --exit-code keeps it honest.
Vendoring respects replacements. If go.mod replaces example.com/lib with ../lib, go mod vendor copies the replacement's code into vendor. After that the build no longer depends on the local path, which is a way to capture a patched dependency in the repository.
| Reason | Vendor? |
|---|---|
| Builds must work without network or proxy access | Yes |
| You must review or audit all third-party code in pull requests | Yes |
| Plain application with a reliable module proxy | Usually no |
| Library published for others | No, consumers do not use your vendor folder |
The default module cache and go.sum checks already give reproducible builds. Vendoring adds repository size and review noise, so use it for a concrete need.
Yes, in the main module the replacement applies to the whole build, including transitive imports of that module.
You can write it, but it has no effect on projects that depend on your library. Publish fixes upstream or tell users to add their own replace.
No. The next go mod vendor overwrites it, and the build checks it against modules.txt. Patch a fork and use replace instead.
Change the version with go get module@version, then run go mod tidy and go mod vendor.
replace redirects a module to a local folder or a fork, but only in the main module and not for your users. go mod vendor copies the packages your build needs into vendor, which -mod=vendor reads without network access. Use replace for temporary patches, vendor when offline builds or code review require it, and keep both consistent with go.mod.