Using Go Modules in Production: Best Practices

Table of Contents

  1. Introduction
  2. Prerequisites
  3. Setup
  4. Creating a New Go Module
  5. Adding Dependencies
  6. Updating Dependencies
  7. Vendoring Dependencies
  8. Building and Distributing the Application
  9. Conclusion

Introduction

In this tutorial, we will explore the best practices for using Go modules in production. Go modules provide a reliable way to manage dependencies and ensure reproducibility of builds. By the end of this tutorial, you will learn how to create a new Go module, add dependencies, update dependencies, vendor dependencies, and build and distribute your application.

Prerequisites

Before starting this tutorial, make sure you have the following prerequisites:

  • Basic understanding of the Go programming language
  • Go installed on your system

Setup

To use Go modules, you need to enable module support in Go. This can be achieved by setting the GO111MODULE environment variable to on, auto, or off. In most cases, it is recommended to set it to auto to enable Go modules for projects outside of the $GOPATH and disable it for projects within the $GOPATH.

You can set the GO111MODULE environment variable using the following command:

export GO111MODULE=auto

Creating a New Go Module

To create a new Go module, follow these steps:

  1. Create a new directory for your project:

     mkdir myproject
     cd myproject
    
  2. Initialize the project as a Go module:

     go mod init github.com/your-username/myproject
    

    The initialization command creates a go.mod file in the root of your project. This file will track your dependencies and their versions.

Adding Dependencies

To add a dependency to your Go module, you need to import the desired package in your code. Once you import the package, you can use the go get command to automatically add the dependency to your go.mod file.

For example, let’s add the popular HTTP client package github.com/go-resty/resty to our project:

go get github.com/go-resty/resty

The go get command downloads and installs the package, and updates the go.mod and go.sum files with the package information.

Updating Dependencies

Go modules make it easy to update your project’s dependencies to the latest versions. To update all dependencies in your project, use the go get command with the -u flag:

go get -u

This command updates all dependencies to their latest versions and updates the go.mod and go.sum files accordingly.

You can also update a specific dependency to a specific version. Let’s say we want to update the github.com/go-resty/resty package to version v2.0.0:

go get github.com/go-resty/[email protected]

Vendoring Dependencies

Vendoring allows you to include a copy of your project’s dependencies in the project itself, ensuring reproducibility of builds. Go modules have built-in support for vendoring.

To vendor your dependencies, use the following command:

go mod vendor

This command copies all dependencies into a vendor directory within your project.

When building your project, Go will prioritize the use of vendored dependencies over the global ones, ensuring the correct versions are used.

Building and Distributing the Application

To build your Go application using Go modules, you can use the go build command as usual. However, it is recommended to use the -mod=readonly flag to ensure that the build remains reproducible:

go build -mod=readonly

This flag instructs Go to only use the versions specified in the go.mod file and the vendored dependencies.

To distribute your application, you can simply package the executable file along with the project’s go.mod and go.sum files. This way, anyone can build the project and get the exact same dependencies.

Conclusion

Congratulations! You have learned the best practices for using Go modules in production. You now know how to create a new Go module, add and update dependencies, vendor dependencies, and build and distribute your application.

By following these best practices, you can ensure reproducibility of your Go builds and manage dependencies efficiently in your production projects. Happy coding with Go modules!