Go CLI Modules and Workspaces

Go modules and go work workflows for init, tidy, vendor, replace, workspaces, private modules, and dependency management.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all
## Modules Basics
Initialize module
go mod init example.com/myapp

# Create a new go.mod for the current project.

Tidy dependencies
go mod tidy

# Add missing and remove unused module requirements.

Download dependencies
go mod download

# Pre-fetch required modules into the module cache.

Print module graph
go mod graph

# Show the module dependency graph.

Explain dependency
go mod why -m github.com/pkg/errors

# Show why a package or module is needed.

## Editing and Replacements
Add replace directive
go mod edit -replace example.com/lib=../lib

# Point a module dependency to a local checkout.

Add require directive
go mod edit -require example.com/lib@v1.2.3

# Add an explicit module requirement.

Set go line
go mod edit -go=1.25

# Set the minimum Go version in go.mod.

Vendor dependencies
go mod vendor

# Copy dependencies into a vendor directory.

Inspect current module JSON
go list -m -json

# Inspect current module metadata.

## Dependency Updates
Upgrade dependency
go get example.com/lib@latest

# Update a dependency to the latest version.

Upgrade patch release
go get -u=patch ./...

# Update a dependency within its compatible patch/minor range.

List available updates
go list -m -u all

# Show modules that have newer versions available.

Verify module cache
go mod verify

# Verify dependencies in module cache against go.sum.

Exclude bad version
go mod edit -exclude example.com/lib@v1.2.4

# Exclude a dependency version from selection.

## Private Modules
Set GOPRIVATE
go env -w GOPRIVATE=github.com/myorg/*

# Mark private module path patterns.

Set GONOPROXY
go env -w GONOPROXY=github.com/myorg/*

# Bypass proxy for selected module path patterns.

Set GONOSUMDB
go env -w GONOSUMDB=github.com/myorg/*

# Skip checksum database for selected private modules.

Control allowed VCS
go env -w GOVCS=github.com:git,private:all

# Restrict version control systems used for module fetches.

## Workspaces
Initialize workspace
go work init

# Create a go.work file for multiple local modules.

Add module to workspace
go work use ./app ./lib

# Add one or more modules to go.work.

Print go.work JSON
go work edit -json

# Inspect go.work configuration as JSON.

Sync workspace build list
go work sync

# Sync workspace dependencies back to module go.mod files.

Recursively add modules
go work use -r .

# Add modules under a tree recursively.

Recommended next

No recommendations yet.