Visualize and validate package dependencies in your Go project.
goimportmaps is a CLI tool that analyzes your Go project's internal package imports, visualizes them as a dependency
graph, and detects architectural violations like forbidden dependencies or unexpected imports.
Ideal for large-scale or monorepo Go applications, this tool helps ensure architectural integrity by preventing undesired package-level coupling.
- π Visualize internal package dependencies (Mermaid, Graphviz, HTML)
- π¨ Detect forbidden imports based on custom rules (
forbiddenmode) - π‘ Enforce allowed imports strictly (
allowedmode, whitelist style) - π Coupling metrics analysis (inspired by NDepend)
- Afferent Coupling (Ca): Number of packages depending on this package
- Efferent Coupling (Ce): Number of packages this package depends on
- Instability (I): Ce / (Ca + Ce) ratio (0=stable, 1=unstable)
- β Output violations with actionable messages
- π Highlight architectural drift in pull requests
- π§ Perfect for layered, hexagonal, or clean architecture
# Install goimportmaps into your project
go get -tool github.com/mickamy/goimportmaps/cmd/goimportmaps@latest
# or install it globally
go install github.com/mickamy/goimportmaps/cmd/goimportmaps@latestgoimportmaps ./...You can also scope the analysis to specific subdirectories:
goimportmaps ./internal/...| Option | Description |
|---|---|
--format |
Output format: text, mermaid, html, or graphviz |
--mode |
Validation mode: forbidden (default) or allowed |
--metrics |
Show coupling metrics (overrides config setting) |
Given the following structure:
main
βββ handler
β βββ user_handler.go (imports service)
βββ service
β βββ user_service.go (imports infra)
βββ infra
βββ db.go
If handler imports infra directly, the tool will detect:
π¨ 1 violation(s) found
π¨ Violation: github.com/your/project/internal/handler imports github.com/your/project/internal/infraYou can enforce exact allowed imports:
allowed:
- source: github.com/your/project/internal/handler
imports:
- github.com/your/project/internal/service
stdlib: true # allow importing standard library packages as well (default: true)If handler tries to import anything other than service or stdlib (e.g., infra), it will be flagged.
graph TD
main --> handler
handler --> service
service --> infra
handler --> infra %% βDisplay package coupling metrics to identify architectural issues:
# Show metrics in CLI output
goimportmaps ./... --metrics
# Generate HTML report with metrics dashboard
goimportmaps ./... --format=html --metrics > metrics-report.htmlπ Coupling Metrics:
Package Ca Ce I Status
internal/cli 0 11 1.00 π¨
internal/prints 2 11 0.85 π¨
internal/config 4 6 0.60 β
π¨ Coupling Violations:
- internal/cli: High efferent coupling (11 > 10), High instability (1.00 > 0.80)
- internal/prints: High efferent coupling (11 > 10), High instability (0.85 > 0.80)
- Ca (Afferent Coupling): Higher values indicate packages that are heavily depended upon (stable)
- Ce (Efferent Coupling): Higher values indicate packages with many dependencies (unstable)
- I (Instability):
- 0.0 = Completely stable (only used by others)
- 1.0 = Completely unstable (only uses others)
- 0.5 = Balanced coupling
.goimportmaps.yaml
forbidden:
- source: github.com/your/project/internal/handler
imports:
- github.com/your/project/internal/infra
- source: github.com/your/project/internal/app
imports:
- github.com/your/project/internal/dballowed:
- source: github.com/your/project/internal/handler
imports:
- github.com/your/project/internal/service
- source: github.com/your/project/internal/service
imports:
- github.com/your/project/internal/repository
stdlib: falsemetrics:
enabled: true
coupling:
max_efferent: 10 # Maximum efferent coupling (Ce)
max_afferent: 15 # Maximum afferent coupling (Ca)
max_instability: 0.8 # Maximum instability (I)
warn_efferent: 7 # Warning threshold for Ce
warn_afferent: 10 # Warning threshold for Ca
warn_instability: 0.6 # Warning threshold for IUse --format=html to generate a standalone static report:
# Basic HTML report
goimportmaps ./... --format=html > report.html
# HTML report with metrics dashboard
goimportmaps ./... --format=html --metrics > metrics-report.htmlThe metrics-enabled HTML report includes:
- Interactive dependency graph with Mermaid.js
- Coupling metrics table with color-coded violations
- Visual instability bars and status indicators
- Summary cards showing violation counts
Reports can be viewed in your browser or uploaded as CI artifacts.