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 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

1

Initialize a module in the current directory:

go mod init github.com/you/yourapp
2

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.

3

Add a specific dependency explicitly:

go get github.com/go-chi/chi/v5

Then tidy again if needed:

go mod tidy

Need Go locally? See the local environment guide.


🧩 Minimal Example

go.mod
module github.com/you/yourapp

go 1.22

📦 Adding Dependencies

To add a new dependency, either:

1

Import it in your source file then run go mod tidy, or

2

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 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:

go.mod
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