go.mod
Learn how to generate and configure the go.mod file to manage modules and dependencies for Go apps on Discloud.
🗂️ What is go.mod
?
go.mod
?go.mod
defines your module path, the Go toolchain version (in major.minor
form), and the direct dependencies required by your project. Discloud uses it (and the accompanying go.sum
) to download and verify modules before building your application.
🛠️ Creating a New Module
Initialize a module in the current directory:
go mod init github.com/you/yourapp
Add (or update) dependencies automatically by referencing them in code and tidying:
go mod tidy
This creates go.sum
(must not be empty) with checksum lines.
Add a specific dependency explicitly:
go get github.com/go-chi/chi/v5
Then tidy again if needed:
go mod tidy
🧩 Minimal Example
module github.com/you/yourapp
go 1.22
The Go version MUST be in major.minor
form (e.g. 1.22
). Do NOT use patch: 1.22.3
.
📦 Adding Dependencies
To add a new dependency, either:
Import it in your source file then run go mod tidy
, or
Run go get module/path@version
directly.
Example: add Chi router
go get github.com/go-chi/chi/v5
Resulting go.mod
snippet:
require (
github.com/go-chi/chi/v5 v5.0.12 // example version
)
Versions are resolved semantically by the proxy/module system.
🔒 go.sum
Integrity & Empty Case
go.sum
Integrity & Empty Casego.sum
contains cryptographic hashes of every required module version (direct & indirect) to guarantee reproducible builds. It must be committed together with go.mod
.
Regenerate / update it anytime:
go mod tidy
🧪 Application Type Examples
Tabbed examples for common scenarios:
module github.com/you/discordbot
go 1.22
require (
github.com/bwmarrin/discordgo v0.27.1 // example
)
🧰 Common Commands Reference
# Run your app directly
go run .
# Build a binary
go build -o app
# List all modules (direct+indirect)
go list -m all
# Show dependency graph
go mod graph
Last updated