Skip to content

Instantly share code, notes, and snippets.

@creisor
Last active November 22, 2019 15:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save creisor/84985742404372eb38701e5bc543d820 to your computer and use it in GitHub Desktop.
Save creisor/84985742404372eb38701e5bc543d820 to your computer and use it in GitHub Desktop.
just-tell-me-how-to-use-go-modules - one of the most useful blog posts rescued from 404 obliteration

JUST TELL ME HOW TO USE GO MODULES

This was not written by me. It was very useful and then one day I got a 404, so I dug it up from the wayback machine: https://web.archive.org/web/20190425012016/http://www.kablamo.com.au/blog/2018/12/10/just-tell-me-how-to-use-go-modules


I recently started using Go’s new inbuilt vendoring tool, Go Modules, and having come from both govendor and dep, this new tool was more of a change than I expected.

I’m a fan of quick guides – just tell me what to do so I can start using it now. I don’t need an essay on why I should be using it or painful detail on the tool’s inner workings.

Unfortunately, a quick guide like this doesn’t seem to exist, so I’m writing one here. You’re welcome.

PUT YOUR CODE SOMEWHERE OTHER THAN THE GOPATH

You can technically use Go Modules inside the gopath, but you’ll have to manually set GO111MODULES=on in your environment variables.

That being said, the whole point of modules is to not have to put your code in the gopath, so don’t do that.

INITIALISE GO MODULES

Run this command in your go project:

go mod init <modulename>

‘Module name’ is whatever you want to call your module. This name should be fairly unique, because if you have module name clashes you’re gonna have a bad time.

This creates a go.mod file, which you don’t need to touch.

USE GO GET TO ADD THE DEPENDENCIES TO THE GO.MOD FILE

Just run this:

go get -u ./...

This will add all of your project’s current dependencies to the go.mod file and create a go.sum file. You don’t need to touch that file either.

VENDOR THOSE DEPENDENCIES

Run this:

go mod vendor

This will create a vendor folder and add copies of each dependency listed in your go.mod file to that folder. This folder also can’t be edited, so again, there’s no need to touch it.

UPDATE A DEPENDENCY

Just run:

go get -u <repo url>
go mod vendor

This will update the dependency version in the go.mod file, and then update the code in your vendor folder for that dependency.

This should be enough for you to get up and running with Go Modules on your go project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment