liveBook · Manning

Module path

As explained in Go in Action, Second Edition

A module path is the identifier that names a Go module and distinguishes it when resolving versions and imports.

When several Go modules share a repository, each needs a distinct name that other modules can import and depend on. A module path supplies that identifier. It appears in go.mod and forms the import-path prefix for packages in the module. section 11.1

Choose a path that matches how the module will be found:

  • For a module hosted on GitHub or GitLab, match its repository location so the go command can fetch it. section 11.1
  • For separate modules in one repository, include their subdirectories: github.com/YOUR_USERNAME/go-news/domain and github.com/YOUR_USERNAME/go-news/storage identify different modules. section 11.1 section 11.2
  • For a new major version, append a suffix such as /v2 to the module path. section 11.1
cd domain
go mod init github.com/YOUR_USERNAME/go-news/domain

Run go mod init inside the domain directory with the module’s full path. The resulting go.mod declares that path with a module directive. section 11.1

Depending on a sibling module

The storage module can use github.com/YOUR_USERNAME/go-news/domain as an import path and request github.com/YOUR_USERNAME/go-news/domain@v0.0.1 as a dependency. In the multimodule repository, the domain release is tagged domain/v0.0.1, distinguishing its version from releases of sibling modules. section 11.2

Keep the path consistent across go.mod, imports, and dependency commands. go get can add a versioned dependency; importing a package and running go mod tidy can add it automatically. section 11.1

How Go in Action, Second Edition covers it

How deep it goes
Defines the path, shows initialization and dependency use, and notes subdirectory and major-version conventions without detailing resolution internals.
Examples
Uses domain and storage modules under one GitHub repository, with go mod init commands and a domain/v0.0.1 release tag.
What it assumes
Assumes familiarity with Go imports and Git commands while showing how to create a module with go mod init.
What you can do after
Build it from scratch by assigning distinct module paths, initializing go.mod files, and adding a versioned sibling-module dependency.
How it connects
Connects module paths to go.mod, package imports, semantic versioning, Git tags, and multimodule repositories.

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.