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

Newsletter

It's the really early of this site! There is no automated newsletter signup yet. But if you want to stay posted already (thanks!), send a mail to mail@vsupalov.com. Just say hi and that you'd like to get notified about new Gophed Dojo articles. There's also an RSS link in the footer if you prefer!

gopherdojo.com

© 2024 gopherdojo.com. All rights reserved.