Modules

3 min read

Authors
banner

Table of Contents

In this tutorial, we will learn about modules.

What are modules?

Simply defined, 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.

But wait, what is GOPATH?

Well, 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

Like earlier, let's create a new module using go mod init command which creates a new module and initializes the go.mod file that describes it.

$ go mod init example

The important thing to note here is that a Go module can correspond to a Github repository as well if you plan to publish this module. For example:

$ go mod init example

Now, let's explore go.mod which is the file that defines the module's module path and also the import path used for the root directory, and its dependency requirements.

module <name>

go <version>

require (
	...
)

And if we want to add a new dependency, we will use go install command:

$ go install github.com/rs/zerolog

As we can see a go.sum file was also created. This file contains the expected hashes of the content of the new modules.

We can list all the dependencies using go list command as follows:

$ go list -m all

If the dependency is not used, we can simply remove it using go mod tidy command:

$ go mod tidy

Finishing up our discussion on modules, let's also discuss 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.

So, let's reinstall the removed module using go mod tidy.

package main

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

func main() {
	log.Info().Msg("Hello")
}
$ 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
$ 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
© 2024 Karan Pratap Singh