Modules
Go modules are the official dependency management system introduced in Go 1.11 and default in 1.16+.
Introduction
Go modules are the official dependency management system introduced in Go 1.11 and default in 1.16+. A module is defined by go.mod at the repo root, declaring the module path, Go version, and required dependencies with checksums in go.sum.
Commands like go get, go mod tidy, and go mod vendor manage dependencies. Semantic import versioning (v2+ module path suffix) and retract directives handle breaking changes professionally.
Every production Go repo uses modules — interviewers ask about MVS (Minimal Version Selection), go.sum integrity, and private module proxies.
The story
When Cloudflare open-sources a Go library, consumers add require github.com/cloudflare/cfssl v1.6.5 to their go.mod. Semantic versioning and the module proxy ensure reproducible builds — the same checksums in go.sum that CI verifies before deploying to production Kubernetes clusters.
Module boundaries replace the old GOPATH workflow: each repo has its own go.mod, go get upgrades dependencies, and replace directives patch local forks during development.
Understanding the topic
Key concepts
- go mod init module/path creates go.mod.
- require block lists dependencies; go.sum stores cryptographic hashes.
- go get package@version adds/updates dependency.
- go mod tidy adds missing, removes unused modules.
- Semantic versioning: v0/v1 in path; v2+ requires /v2 in module path.
- GOPRIVATE bypasses proxy for internal modules.
Step-by-step explanation
- go mod init sets module path (usually GitHub URL).
- Import third-party package — go mod tidy fetches and records.
- go build reads go.mod, downloads to module cache.
- proxy.golang.org serves public modules; sumdb verifies checksums.
- replace directive points to local fork for development.
- go work combines multiple modules in monorepo.
Practical code example
go.mod with require, retract awareness, and tidy workflow:
// go.modmodule github.com/techlearningpro/paymentsgo 1.22require (github.com/google/uuid v1.6.0github.com/stretchr/testify v1.9.0)// Run: go mod tidy// Run: go get github.com/google/uuid@v1.6.0// main.gopackage mainimport ("fmt""github.com/google/uuid")func main() {id := uuid.New()fmt.Println("payment id:", id)}
Line-by-line code explanation
module github.com/techlearningpro/paymentsis the first line of go.mod. It gives the module its path, which is the prefix other code uses to import packages from this module.go 1.22sets the Go language version this module expects.require (opens the block that lists the external modules this module depends on, each with a pinned version.github.com/google/uuid v1.6.0says the project depends on the uuid module at version v1.6.0. This is the module that main.go imports.github.com/stretchr/testify v1.9.0lists a testing library. Nothing in main.go imports it, and go mod tidy would remove it unless some other file in the module uses it.// Run: go mod tidyis only a comment that tells the reader to run that command. It adds missing requirements and removes ones that are no longer used.// Run: go get github.com/google/uuid@v1.6.0is also just a comment. The command would add that module at exactly that version to go.mod."github.com/google/uuid"is the import in main.go. Go finds this package through the require line in go.mod.id := uuid.New()generates a new random UUID. The next line,fmt.Println("payment id:", id), prints payment id: followed by the UUID as text, which is different on every run.
Key takeaway: The go.mod file names the module and lists every dependency with an exact version. The import in main.go has to match a require line, which is what go mod tidy keeps in sync. Pinning versions makes builds repeatable on every machine.
Real-world use
Where you'll use this in production
- Pinning dependency versions for reproducible CI builds.
- Monorepo with go.work linking api and worker modules.
- Vendoring dependencies for air-gapped deployments.
- Private module hosting on GitHub/GitLab with GOPRIVATE.
Best practices
- Run go mod tidy before every commit touching imports.
- Commit go.mod and go.sum together.
- Use semantic versioning tags for your published modules.
- Set GOPRIVATE for internal module paths.
- Review go get upgrades in dedicated PRs with test coverage.
- Document minimum Go version in go.mod go directive.
Common mistakes
- Deleting go.sum entries manually — run go mod tidy instead.
- Importing v2 module without /v2 in path — compile errors.
- Not committing go.sum — CI builds fail or become insecure.
- replace left in production go.mod pointing to local paths.
- Using @latest in production without pinning in require.
Advanced interview questions
Interview Prep
Practice concise answers, then expand each card for the explanation.
1BeginnerQuestiongo.mod vs go.sum?+
Answer
2BeginnerQuestiongo mod tidy purpose?+
Answer
3IntermediateQuestionMinimal Version Selection?+
Answer
4IntermediateQuestionImport v2 breaking module?+
Answer
5AdvancedQuestionManage private modules in enterprise.+
Answer
Summary
go.mod defines module path, Go version, and dependencies. go mod tidy keeps dependencies clean; commit go.sum always. v2+ modules require /v2 suffix in import path. GOPRIVATE and go.work support enterprise monorepos. Next lesson: goroutines — lightweight concurrency.