Last updated August 30, 2024
This guide outlines how to manage the Go toolchain and dependencies on Heroku using Go Modules.
The Using Go Modules blog post covers the most common usage of the tool. We cover the most common activities in this article.
Build Configuration
go.mod Directives
When pushing code that uses Go modules, Heroku uses several directives in the go.mod file to configure your build. These directives are:
go: This directive is usually at the top of thego.modfile. It describes the minimum Go version that can be used to compile the app. This value is used to select the version of Go to install on Heroku at build time. This value doesn’t include a minor version. Heroku resolves the minor version to the latest available. For example:go 1.21require: This field defines the module and minimum versions of those modules that must be installed. These modules are downloaded, unless they’re already in thevendor/directory. For example:require github.com/gin-gonic/gin v1.10.0
go.mod Heroku Build Directives
Additionally, Heroku supports build directives in the go.mod file.
// +heroku goVersion <version>: You can use this field to specify a different Go version than specified by the go directive. Using this directive you can override thegodirective for Go version selection. For example:// +heroku goVersion go1.22.6// +heroku install <packagespec>[ <packagespec>]: You can use a space separated list of the packages you want to install here. If not specified, Heroku defaults to detecting and installing themainpackages in the code base. If this process isn’t what you want, you can instruct the buildpack to only build certain packages via this option. Other common choices are:./cmd/...(all packages and sub packages in the cmd directory) and./...(all packages and sub packages of the current directory). For example:// +heroku install ./cmd/... ./special
Go Module Authentication With go.sum
If a go.sum is provided, the Go toolchain verifies that the installed modules match the expected cryptographic hashes. This file is generated and updated by many go module-aware commands. Heroku recommends committing go.sum to the repository for an extra layer of security.
Go Module Vendoring With vendor/modules.txt
If a vendor/modules.txt is present, the Go toolchain attempts to use modules included in the repository under the /vendor directory rather than downloading them from the internet. You can use this vendoring strategy to reduce build timeouts, errors, or improve build speed. You can generate the required files by running go mod vendor, then committing the result.
Install or Update Go Modules
Go modules are included with the Go toolchain. See the instructions for installing the Go toolchain.
Project Initialization
- Run
go mod init <project>to generatego.mod. - Run
go mod tidyto install dependent modules and generate ago.sum. - Optionally run
go mod vendorto vendor dependencies if you want to vendor modules. - Inspect the changes with
git diffor a similar command. - Commit the changes with
git add -A; git commit -am "Setup Go modules"
Adding Dependencies
- Run
go get -u <module>. For example:go get -u github.com/gin-gonic/gin. - Add
import <module>directives in your.gofiles where needed. - Inspect the changes with
git diffor a similar command. - Commit the changes with
git add -A vendor; git commit -am "Add dependency <package>"
Dependency Status
- Run
go mod graphto get a summary of modules. - Run
go mod verifyto check module authenticity.
Updating an Existing Dependency
- Run
go get <module>orgo get <module>@<version>. - Inspect the changes with
git diffor a similar command. - Commit the changes with
git add -A vendor; git commit -m "Update <module>".
Removing Unused Dependencies
- Run
go mod tidy - Inspect the changes with
git diffor a similar command. - Commit the changes with
git add -A vendor; git commit -m "Remove unused dependencies".