vsupalov

Switching to Go Modules - bye GOPATH and Godep

Go modules! Finally you don’t have to place your code into the directory structure imposed by GOPATH! Also, you don’t need to set up external tooling, such as Godep, to manage your project’s dependencies.

All you need is to use Go in the version 1.11+ and you’re set.

Well, not quite.

Before you can start enjoying modules, you need to make the switch to them, or init your fresh new project to use them. Here’s how:

Apply it to an old project

You have an existing project, and want to start using go modules? Run the following command, and you’re almost done:

go mod init github.com/name/repo

The last argument, is the importable path to the directory you’re working from.

If you are already using godep, you won’t need to do anything. The versions you specified prior, are copied over from your Godep files. All that’s left to do, is to delete the old Godeps.* files once the command is done.

When you’re starting from scratch

If you haven’t been using Godep on an old project, or are starting a new Go project and would like to use modules, all you need is a single command.

After the mod init command like above, you can use go get to add dependencies to the project, and have the module remember their exact version.

Additional Resources

Subscribe to my newsletter!
You'll get notified via e-mail when new articles are published. I mostly write about Docker, Kubernetes, automation and building stuff on the web. Sometimes other topics sneak in as well.

Your e-mail address will be used to send out summary emails about new articles, at most weekly. You can unsubscribe from the newsletter at any time.

We use rapidmail to send our newsletter. When you subscribe, you consent to the entered data being forwarded to rapidmail. Please see the rapidmail GTC and data privacy statement. .