Package Management With Go Modules: The Pragmatic Guide

Quick Start

If your project is already in version control, you can simply run

go mod init
go mod init github.com/you/hello
module github.com/you/hello

go 1.12
# use Git tags
go get github.com/go-chi/chi@v4.0.1
# or Git branch name
go get github.com/go-chi/chi@master
# or Git commit hash
go get github.com/go-chi/chi@08c92af
module github.com/you/hello

go 1.12

require github.com/go-chi/chi v4.0.2+incompatible // indirect
go mod tidy

Updating Package Versions

You may use go get -u or go get -u=patch to update dependencies to the latest minor or patch upgrades respectively.

  • Follow semver (an example VCS tag is v1.2.3).
  • If the module is version v2 or higher, the major version of the module must be included as a /vN at the end of the module paths used in go.mod files and in the package import path:
import "github.com/you/hello/v2"

Substitute Imported Modules

You can point a required module to your own fork or even local file path using replace directive:

go mod edit -replace github.com/go-chi/chi=./packages/chi
module github.com/you/hello

go 1.12

require github.com/go-chi/chi v4.0.2+incompatible
replace github.com/go-chi/chi => ./packages/chi
go mod edit -dropreplace github.com/go-chi/chi

Managing Dependencies Per Project

Historically, all Go code was stored in one giant monorepo, because that’s how Google organizes their codebase internally and that took its toll on the design of the language.

Option 1: Set GOPATH inside your project directory

This might sound counterintuitive at first, but if are running Go from a container, you can override its GOPATH to point to the project directory so that the packages are accessible from the host:

Option 2: Vendor Your Dependencies

Another way is to copy over your project dependencies to vendor folder:

go mod vendor
  1. Go will rebuild the download cache for all packages at $GOPATH/pkg/mod/cache.
  2. All downloaded modules will be copied over to $GOPATH/pkg/mod.
  3. Finally, Go will copy over these modules to vendor folder while pruning examples, tests and some other miscellaneous files that you do not directly depend on.
go get github.com/rs/zerolog@v1.14.3
import (
_ "github.com/rs/zerolog"
)
go mod vendor

Tool Dependencies

We might want to install some go-based tools that are not being imported, but are used as part of project’s development environment. A simple example of such tool is CompileDaemon that can watch your code for changes and restart your app.

// +build toolspackage toolsimport (
_ "github.com/githubnemo/CompileDaemon"
)
  • // +build tools constraint prevents your normal builds from actually importing your tool
  • import statements allow the go command to precisely record the version information for your tools in your module’s go.mod file

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store