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

  1. 01

    Module

    go
    module example.com/tools

    Declared in go.mod

  2. 02

    Directory

    go
    format/

    Package location below the module root

  3. 03

    Import

    go
    example.com/tools/format

    Module 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.

Official reference ↗

Read and predict

go
// 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?

Try it yourself

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.