0Pricing
Go Academy · Lesson

Go Modules with go mod

Initializing modules, go.mod, go.sum

Go Modules with go mod is a free Go Academy lesson on CoddyKit — lesson 2 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.

What Is a Go Module?

A module is a collection of Go packages with a go.mod file at its root. Modules are the unit of code distribution and versioning in Go:

// go.mod — created with: go mod init github.com/myuser/myapp
module github.com/myuser/myapp

go 1.21

require (
    github.com/gin-gonic/gin v1.9.1
    golang.org/x/sync v0.5.0
)

go mod init

Initialize a new module with go mod init:

# In project directory:
go mod init github.com/myuser/myapp

# This creates go.mod:
# module github.com/myuser/myapp
# go 1.21

# The module path is the canonical import path prefix
# for all packages in this module

go.sum — Integrity Verification

go.sum records cryptographic checksums of all dependencies. Never edit it manually — go commands maintain it:

# go.sum example entries:
github.com/gin-gonic/gin v1.9.1 h1:4idEAncQnU5cB7BeOkPtxjfCSye0AAm1R0RVIqJ+Jmg=
github.com/gin-gonic/gin v1.9.1/go.mod h1:hPrL7YrpYKXt5YId3A/Tnip5kqbEAP+KLuI3SUcPTeU=

# Commit go.sum to version control — it ensures reproducible builds

go get — Adding Dependencies

Use go get to add or update dependencies. It updates both go.mod and go.sum:

# Add latest version
go get github.com/pkg/errors

# Add specific version
go get github.com/pkg/errors@v0.9.1

# Upgrade to latest patch release
go get -u=patch github.com/pkg/errors

# Remove unused dependency from go.mod
go mod tidy

go mod tidy

go mod tidy adds missing and removes unused requirements from go.mod and go.sum. Run it after editing imports:

# After adding/removing imports:
go mod tidy

# What it does:
# 1. Adds any missing module requirements
# 2. Removes requirements for packages no longer imported
# 3. Updates go.sum with needed checksums
# 4. Best practice: run before committing

Module Versioning with Semver

Go modules use semantic versioning. Major versions >= 2 require a path suffix:

// v1: github.com/pkg/errors
import "github.com/pkg/errors"

// v2+: module path includes major version
import "github.com/foo/bar/v2"
import "github.com/foo/bar/v3"

// go.mod:
// require github.com/foo/bar/v2 v2.3.1

go mod vendor

Vendor mode copies all dependencies into a local vendor/ directory for offline builds:

# Copy deps to vendor/
go mod vendor

# Build using vendor/
go build -mod=vendor ./...

# Useful for:
# - Air-gapped environments
# - Reproducible CI builds without network
# - Code review of dependencies

replace Directive

The replace directive in go.mod substitutes a module with a local path or different version — useful for local development:

module github.com/myapp

go 1.21

require github.com/mylib v1.0.0

// Use local fork during development:
replace github.com/mylib => ../mylib

// Replace with a specific version:
// replace github.com/mylib v1.0.0 => github.com/myfork v1.0.0

go env GOPATH and GOMODCACHE

Key environment variables for Go modules:

# Module cache location (default: ~/go/pkg/mod)
go env GOMODCACHE

# Proxy for downloading modules (default: proxy.golang.org)
go env GOPROXY

# Sum database for checksum verification
go env GONOSUMCHECK

# Disable module proxy (use direct)
export GOPROXY=direct

go list and go mod graph

Inspect module dependencies with these commands:

# List all modules in the build
go list -m all

# Show module dependency graph
go mod graph

# Find which module provides a package
go list -m -json all | grep -A5 gin

# Check for available updates
go list -m -u all

Quick Check

What does go mod tidy do?

Recap: Go Modules

Key module concepts:

  • go mod init creates the module with a go.mod file
  • go.mod lists module name, Go version, and dependencies
  • go.sum holds checksums — commit it
  • go get adds/updates deps, go mod tidy cleans up
  • replace for local development forks
  • Major versions v2+ require path suffix

Frequently asked questions

Is the “Go Modules with go mod” lesson free?

Yes — the full text of “Go Modules with go mod” 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 “Go Modules with go mod”?

Initializing modules, go.mod, go.sum 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 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Go Modules with go mod” 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

  1. Packages: Organizing Go Code
  2. Go Modules with go mod
  3. Adding External Dependencies
  4. Internal Packages and Workspaces
← Back to Go Academy