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


