Managing Go Modules
bashbunni
Posted on December 9, 2021
Managing Dependencies with go.mod
A project's dependencies are declared in the go.mod file. This is where the modules are stored when you do go get <module name>
. You can remove unused dependencies using go mod tidy
and your go.mod will only include all used imports.
When you clone a Go project from github, for example, you'll want to run go get
to install all of the dependencies declared in the go.mod file. If you're coming from a background in web development, this is similar to how you run npm install
to install all of the dependencies declared in your package.json
file.
Indirect Dependencies
You may see some dependencies in your go.mod file that are indirect (denoted by // indirect
). This means that one of your dependencies doesn't have its own go.mod file, so, the dependencies that it imports are included in your project's go.mod file.
For example:
Let's say we're building a Twitch bot, we need github.com/gempir/go-twitch-irc/v2
, but it doesn't have a go.mod file and it uses functions from github.com/gempir/go-twitch-irc
. You would then have github.com/gempir/go-twitch-irc // indirect
in your project's go.mod file.
note: the above is a hypothetical scenario, you won't actually have any indirect dependencies with github.com/gempir/go-twitch-irc/v2
What is go.sum?
go.sum includes the checksum for the dependencies to make sure that you're installing the exact same dependency as the author used.
Managing your Projects as Go Modules
To create a new Go project where you can have a go.mod file, you need to set it up on your GOPATH with a name for your project.
Running go mod init <module name>
Your module name can be your project name or it can be a link to your github repo, just remove the https:// portion of the url.
You need to run go mod init
before you're able to get dependencies for your project with go get
Posted on December 9, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.