liveBook · Manning

Go.mod file

As explained in Go in Action, Second Edition

A go.mod file is a Go module definition file that records the module path, Go language version, and module dependencies.

A go.mod file defines a Go module at a project root. The Go toolchain uses it when building code in that directory and its subdirectories. A module groups related packages and records the dependencies needed to build them. section 2.3.2 section 11.0

Create the file from the module’s directory with `go mod init gobook/wordcount`. A new module with no external dependencies needs only a `module` directive for its path and a `go` directive for its Go version. The module path also becomes the import-path prefix for packages in the module. section 2.3.2 section 11.1

Listing 2.2 Your new go.mod file
module gobook/wordcount

go 1.25

The initial wordcount file shows the two directives before any dependencies have been added. section 2.3.2

For a hosted module, choose a path matching its repository location so the `go` command can fetch it. A dependency-bearing go.mod adds `require` entries with module versions; entries marked `// indirect` identify indirect dependencies. The file is readable and editable, but commands such as `go get` and `go mod tidy` normally manage changes as dependencies are added. section 11.1

module github.com/YOUR_USERNAME/go-news/api #1

go 1.25 #2

require ( #3
    github.com/YOUR_USERNAME/go-news/domain v0.0.1
    github.com/gorilla/mux v1.8.0
)

require (
    github.com/other/transitive v0.5.2 // indirect #4
)

The API module’s file illustrates versioned requirements for a shared domain module and an external package, plus an indirect requirement. section 11.1

Keep go.mod alongside go.sum: go.mod identifies the module and its requirements, while go.sum holds checksums used to detect changes to downloaded dependencies. Separately managed modules can let shared components and services change independently; a workspace can coordinate multiple modules during development. section 11.1 section 11.0

How Go in Action, Second Edition covers it

How deep it goes
Shows initialization, file directives, versioned requirements, and dependency commands; does not detail the toolchain’s dependency-resolution internals.
Examples
Uses a wordcount module and a multi-module API project with direct and indirect requirements.
What it assumes
Starts from a new project directory and teaches module initialization without assuming an existing go.mod file.
What you can do after
Operate/configure it with `go mod init`, `go get`, and `go mod tidy` to establish a module and manage requirements.
How it connects
Links modules to packages, import paths, dependencies, go.sum, and workspaces.

Also explained in

Compare how 3 books explain go.mod file →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Go in Action, Second Edition. The section links point to the parts of the book it draws on.