Adding External Dependencies
go get, versioning, and the module cache
Adding External Dependencies is a free Go Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Finding Packages
The primary resource for finding Go packages is pkg.go.dev. Search for packages, read docs, and see usage examples:
# Search and explore at https://pkg.go.dev
# Popular packages:
# github.com/gin-gonic/gin — HTTP framework
# github.com/stretchr/testify — test assertions
# golang.org/x/sync — extended sync primitives
# github.com/spf13/cobra — CLI framework
# github.com/jackc/pgx/v5 — PostgreSQL drivergo get: Adding a Dependency
Use go get to add a dependency. It downloads the module and updates go.mod and go.sum:
# Add latest version
go get github.com/stretchr/testify
# Add specific version
go get github.com/stretchr/testify@v1.8.4
# Add a pre-release version
go get github.com/foo/bar@main
# go get updates go.mod:
# require github.com/stretchr/testify v1.8.4Using the Dependency
After go get, import and use the package normally:
package main
import (
"fmt"
"github.com/stretchr/testify/assert"
"testing"
)
func TestAdd(t *testing.T) {
assert.Equal(t, 4, 2+2, "math should work")
fmt.Println("assertion done")
_ = assert.New // suppress unused import
}go mod download
Pre-download all dependencies listed in go.mod to the module cache:
# Download all deps from go.mod
go mod download
# Download specific module
go mod download github.com/gin-gonic/gin@v1.9.1
# Useful for:
# - Docker layer caching (copy go.mod/go.sum first, then download)
# - Pre-warming CI cache
# - Offline development setupSemantic Version Selection: MVS
Go uses Minimum Version Selection (MVS): it always picks the minimum required version that satisfies all dependencies. No automatic major upgrades:
# If A requires gin v1.9.0
# and B requires gin v1.9.1
# MVS selects v1.9.1 (minimum that satisfies both)
# To upgrade ALL dependencies to latest minor/patch:
go get -u ./...
# To upgrade specific package:
go get -u github.com/gin-gonic/gingo mod why — Explaining Dependencies
Find out why a module is required in your build:
# Why is encoding/json needed?
go mod why encoding/json
# Why is golang.org/x/net needed?
go mod why golang.org/x/net
# Output shows the import chain:
# github.com/myapp
# github.com/gin-gonic/gin
# golang.org/x/netgo mod vendor for Reproducible Builds
Vendoring locks all dependencies into your repo for reproducible builds:
# Vendor all deps
go mod vendor
# Build only from vendor (no network)
go build -mod=vendor ./...
# Dockerfile pattern:
# COPY go.mod go.sum ./
# RUN go mod download
# COPY . .
# RUN go build ...Private Modules with GONOSUMCHECK and GOPRIVATE
Configure Go for private modules hosted on internal servers:
# Tell Go not to use proxy/sumdb for private modules
export GOPRIVATE=github.com/mycompany/*
export GONOSUMCHECK=github.com/mycompany/*
# Or in go env:
go env -w GOPRIVATE=github.com/mycompany/*
# For enterprise Go proxy:
export GOPROXY=https://proxy.mycompany.com,directgo clean -modcache
Clear the module cache to force re-downloading all dependencies:
# Clear entire module cache
go clean -modcache
# Clear cache for specific version
go clean -modcache github.com/gin-gonic/gin@v1.9.1
# Useful when:
# - Cache is corrupted
# - Testing with a fresh environment
# - Disk space cleanupChecking Vulnerabilities: govulncheck
The official Go vulnerability scanner checks your dependencies for known CVEs:
# Install govulncheck
go install golang.org/x/vuln/cmd/govulncheck@latest
# Scan your module
govulncheck ./...
# Output example:
# Vulnerability #1: GO-2023-1234
# github.com/foo/bar v1.2.3
# Fixed in: v1.2.4
# Suggestion: go get github.com/foo/bar@v1.2.4Quick Check
Which command adds a specific version of a dependency to your module?
Recap: External Dependencies
Summary:
go get pkg@versionadds dependencies- Go uses MVS — minimum version that satisfies all requirements
go mod tidycleans unused depsgo mod vendorfor fully reproducible offline builds- Use
GOPRIVATEfor private modules - Run
govulncheckto find vulnerabilities
Frequently asked questions
Is the “Adding External Dependencies” lesson free?
Yes — the full text of “Adding External Dependencies” is free to read here on the web, and the Go Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Go Academy course, upgrade to CoddyKit PRO.
What will I learn in “Adding External Dependencies”?
go get, versioning, and the module cache You practise Go Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Go Academy?
No prior experience is required. Go Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Adding External Dependencies” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Go Academy lesson?
Yes. Every Go Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Packages: Organizing Go Code
- Go Modules with go mod
- Adding External Dependencies
- Internal Packages and Workspaces