Go Module Cheat Sheet

I've recently had occasion to do a fair amount of work with Go modules and vendoring, and I've had trouble finding simple how-tos for doing some (to me) basic things. It turns out they aren't that complicated, but they do involve multiple systems working together which may not be obvious to Go newbies (like me!). I'm writing this down both for my own reference and in the hopes that maybe it will help someone else get started with this stuff. I make no promises as to the accuracy of these steps, but they did what I needed.

Here are some things you might want to do and how to do them, all run from the directory of the project doing the vendoring:

  • Update version of a single vendored module

    For pulling in new features or bugfixes.
    go get github.com/example/module
    go mod vendor

  • Update version of a single vendored module to a specific branch/tag

    For pulling in new features or bugfixes on branches
    go get github.com/example/module@branch
    go mod vendor

  • Update all vendored modules

    go get -u
    go mod vendor

  • Make a project use a local copy of a vendored module

    For when you need to test a local change that is vendored in another project.
    go mod edit -replace github.com/example/module=/path/to/local/source
    go mod vendor

Looking back at this I guess it's pretty clear that the whole process boils down to "do a thing that affects the module config of the project then run go mod vendor", but again I had trouble finding a resource that actually covered those things at the same time. The fact that vendoring has changed rather significantly in Go doesn't help either because half of the documents I found were horribly out of date (as this one will no doubt be in short order ;-).

Anyway, hope this helps. If you have corrections or suggestions for other things to include, let me know.