Packages, modules and tooling · 3 MIN READ
The import path is the module path plus the directory
Predict the import path of any package in a module.
go.mod names the module, such as example.com/forge. A package in internal/build is then imported using example.com/forge/internal/build.
The import path locates the package; its package clause determines the default name used in source. The name often matches the last directory component, but does not have to. An import alias can choose another local name.
An internal directory also enforces an import boundary: only code within the tree rooted at its parent may import that package.
SEE THE IDEA
A package path comes from module plus directory
- 01
Module
gomodule example.com/toolsDeclared in go.mod
- 02
Directory
goformat/Package location below the module root
- 03
Import
goexample.com/tools/formatModule path joined with that directory
The import path locates the package; the package declaration names it in source.
In practice
Keep the distinction between path and local name clear. A package may declare a name different from its directory, and an import alias can choose another local spelling. Import cycles are rejected, so arrange dependencies to flow from higher-level features toward shared lower-level packages.
Watch out
Moving a package directory changes its import path. Renaming only its package clause changes the name used by importing code.
Read and predict
// go.mod
module example.com/forge
go 1.24
// internal/build/build.go
package build
func Run() {}How does cmd/forge/main.go import Run, and could a different module do the same?
Trace each value and write down your reasoning. Copy the source into GopherForge or your local Go environment to compile and check it. This web page does not execute your code or send it to a compiler service.
Progress stays in this browser. No account needed.