Getting Started with Go Modules

Go modules are the dependency management system that replaced the old GOPATH workflow. If you have ever been confused about where Go expects your code to live, modules are the answer: your project can live anywhere, and a single file tracks exactly what it depends on.

Initialize a module

A module is defined by a go.mod file at the root of your project. You create one with the module path, which is conventionally the repository URL where the code will live:

go mod init github.com/yourname/myproject

This writes a go.mod recording the module path and the Go version. That path is also the import prefix for every package inside your project.

Add a dependency

You do not edit go.mod by hand to add packages. You import them in code and let the tooling resolve them:

import "github.com/labstack/echo/v4"

Then run go mod tidy. It scans your imports, downloads what you use, removes what you do not, and records exact versions plus checksums in go.sum. That checksum file is what makes builds reproducible and tamper-evident.

Understand the two files

  • go.mod - your direct dependencies and minimum versions, meant to be read by humans.
  • go.sum - cryptographic checksums for every module in the graph, meant to be verified by machines. Commit both.

Keep it clean

Run go mod tidy before every commit that touches imports. It is the single command that keeps your dependency list honest, and reviewers will thank you for a diff that only contains the packages you actually added.

  • #go
  • #modules
  • #setup