Package Management With Go Modules: The Pragmatic Guide

Alexander Diachenko
Aug 3 · 8 min read

Quick Start

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

import "github.com/you/hello/v2"

Substitute Imported Modules

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

Option 1: Set GOPATH inside your project directory

Option 2: Vendor Your Dependencies

go mod vendor

go get github.com/rs/zerolog@v1.14.3
import (
_ "github.com/rs/zerolog"
)
go mod vendor

Tool Dependencies

// +build toolspackage toolsimport (
_ "github.com/githubnemo/CompileDaemon"
)

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade