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
