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
Initialize a module in the current directory:
go mod init github.com/you/yourappAdd (or update) dependencies automatically by referencing them in code and tidying:
go mod tidyThis creates go.sum (must not be empty) with checksum lines.
Add a specific dependency explicitly:
go get github.com/go-chi/chi/v5Then tidy again if needed:
go mod tidy๐งฉ Minimal Example
module github.com/you/yourapp
go 1.22The 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
Resulting go.mod snippet:
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:
๐งช Application Type Examples
Tabbed examples for common scenarios:
Requirements:
Your HTTP server must listen on
8080.
๐งฐ Common Commands Reference
Last updated