Skip to main content
In this guide, we’ll learn about Go modules and how to manage dependencies in your Go projects.

What are modules?

A module is a collection of Go packages stored in a file tree with a go.mod file at its root, provided the directory is outside $GOPATH/src. Go modules were introduced in Go 1.11, which brings native support for versions and modules. Earlier, we needed the GO111MODULE=on flag to turn on the modules functionality when it was experimental. But now after Go 1.13 modules mode is the default for all development.

What is GOPATH?

GOPATH is a variable that defines the root of your workspace and it contains the following folders:
  • src: contains Go source code organized in a hierarchy.
  • pkg: contains compiled package code.
  • bin: contains compiled binaries and executables.
gopath
Modules provide a better way to manage dependencies compared to the old GOPATH-based approach.

Creating a Module

1

Initialize a new module

Use go mod init command to create a new module and initialize the go.mod file:
$ go mod init example
A Go module can correspond to a Github repository as well if you plan to publish this module:
$ go mod init github.com/username/example
2

Understand go.mod structure

The go.mod file defines the module’s module path (import path for the root directory) and its dependency requirements:
module <name>

go <version>

require (
  ...
)

Managing Dependencies

Installing Dependencies

To add a new dependency, use the go install command:
$ go install github.com/rs/zerolog
When you install a dependency, a go.sum file is also created. This file contains the expected hashes of the content of the modules.

Listing Dependencies

You can list all the dependencies using go list command:
$ go list -m all

Cleaning Up Dependencies

If a dependency is not used, you can remove it using go mod tidy command:
$ go mod tidy
The go mod tidy command adds missing dependencies and removes unused ones automatically.

Vendoring

Vendoring is the act of making your own copy of the 3rd party packages your project is using. Those copies are traditionally placed inside each project and then saved in the project repository. This can be done through go mod vendor command. Let’s see an example:
1

Add a dependency to your code

package main

import "github.com/rs/zerolog/log"

func main() {
  log.Info().Msg("Hello")
}
2

Download the dependency

$ go mod tidy
go: finding module for package github.com/rs/zerolog/log
go: found github.com/rs/zerolog/log in github.com/rs/zerolog v1.26.1
3

Create vendor directory

$ go mod vendor
After the go mod vendor command is executed, a vendor directory will be created:
├── go.mod
├── go.sum
├── go.work
├── main.go
└── vendor
    ├── github.com
    │   └── rs
    │       └── zerolog
    │           └── ...
    └── modules.txt

Common Module Commands

CommandDescription
go mod initInitialize a new module
go installInstall a package and add it to go.mod
go list -m allList all module dependencies
go mod tidyAdd missing and remove unused modules
go mod vendorCreate a vendor directory with dependencies

Build docs developers (and LLMs) love