liveBook · Manning

Go mod init

As explained in Go in Action, Second Edition

go mod init is a Go command that creates a go.mod file and initializes a module in a project directory.

go mod init creates a go.mod file in the current directory and starts a Go module. This lets a project build outside GOPATH while giving its packages a shared import-path prefix and a foundation for versioned dependencies. section 2.3.2 section 11.1

Choose the module path carefully because it:

  • Identifies the module uniquely. section 11.1
  • Becomes the import-path prefix for packages inside the module. section 11.1
  • Should match the repository location for hosted modules, with subdirectories for multimodule repositories and /v2-style suffixes for major versions. section 11.1

Initialize a wordcount module

Run `go mod init gobook/wordcount` from the project directory. Go creates a minimal go.mod containing `module gobook/wordcount` and a Go language version. As dependencies are added, go.mod records their versions; go.sum records cryptographic checksums used for reproducible, tamper-resistant builds. section 2.3.2 section 11.1

Listing 2.1 Initializing the wordcount module
$ go mod init gobook/wordcount 
go: creating new go.mod: module gobook/wordcount

Inspect the command output for a concrete module initialization. section 2.3.2

How Go in Action, Second Edition covers it

How deep it goes
Explains directory initialization, module-path design, and resulting dependency files, but defers detailed dependency commands to go get and go mod tidy.
Examples
Demonstrates initializing gobook/wordcount and github.com/YOUR_USERNAME/go-news/domain modules.
What it assumes
Assumes a Go project directory and basic package familiarity; teaches module initialization and module-path selection.
What you can do after
Operate Go tooling to create a module manifest and establish package import paths for a project.
How it connects
Connects Go Modules with go.mod, go.sum, module paths, dependency management, and reproducible builds.

Also explained in

Compare how 2 books explain go mod init →

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.