Skip to main content
In this lesson you’ll learn what a go.mod file is, how to create one, and how to manage dependencies with Go modules. A directory becomes a Go module when it contains a valid go.mod file in its root. Use go mod init to create this file and record the module path. Example: initialize a module
A minimal go.mod looks like:
  • The file starts with a module declaration (module <module-path>).
  • It commonly includes the minimum supported Go version (here go 1.19).
Make sure the module path you choose is unique and, if you plan to publish the module, matches the repository location (for example github.com/<org>/<repo>). Changing the module path later can be disruptive.
Create main.go that imports a third-party package and uses it. Note the exported function name NewRandom() (capital R):
At this point the code imports a third-party package but you haven’t downloaded it yet. Use:
What go mod tidy does
  • Adds missing module requirements needed to build the module.
  • Removes unused requirements.
  • Updates go.sum to match the resolved dependency graph. This keeps go.mod and go.sum in sync with your source code.
Representative output from go mod tidy:
After running go mod tidy, your go.mod will include resolved dependencies. Example:
Run the program:
Example runtime output:
Quick reference — common Go module commands Links and references Summary
  • A directory is a Go module when it contains a valid go.mod.
  • go.mod contains the module declaration, the minimum Go version, and required module dependencies.
  • Use go mod tidy frequently to keep go.mod and go.sum consistent with your code.
  • The replace directive can be used in go.mod to override module versions or point to local copies during development.
Use go mod tidy frequently during development to ensure go.mod and go.sum stay in sync with your source code.

Watch Video