By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
A package is a directory of Go files compiled together. Its name is the last element of its import path, and everything exported starts with a capital letter. Package boundaries are your real architecture: if the analyzer package cannot import the HTTP package, the dependency arrow points the direction you drew. Import cycles are compile errors, which means the compiler enforces your layering for free.
A module is a versioned collection of packages with one go.mod at its root. Relay is one module (relay) with several packages. The internal/ directory is special: packages under internal/ can only be imported by code inside the same module, so they cannot leak into someone else's dependency graph. cmd/relay and cmd/relayd are thin main packages that read configuration, wire dependencies and call into internal/.
The layout below is the target for the rest of the path. You do not create every directory at once; each section adds the package it needs. The key invariant is direction: cmd imports internal/httpapi, which imports internal/analyze and internal/store — never the other way around.
# go.mod is created once, then edited only when the module changes.
cat go.mod
# List every package in the module (directories, not files).
go list ./...
# After adding or removing imports, align go.mod and go.sum.
go mod tidy
# Check which packages import what; look for surprising arrows.
go list -deps ./cmd/relayd | head -20| Directory | Contains | May import |
|---|---|---|
cmd/relay | CLI flags, wiring, main | internal/analyze, internal/config |
cmd/relayd | HTTP server wiring, main | internal/httpapi, internal/store |
internal/httpapi | Routes, middleware, handlers | internal/analyze, internal/ingest |
internal/analyze | Parsing, aggregation, domain types | Standard library only |
internal/store | Repository interfaces and SQL | internal/analyze for types |
internal/observability | Loggers, metrics, trace helpers | Standard library and log/slog |
testdata/ | Fixture logs and golden files | Ignored by the compiler |
Key takeaways
Packages are the unit of design; import paths are the dependency graph and cycles are compile errors.
A module is one go.mod; internal/ packages cannot be imported from outside the module.
Keep main packages thin: parse flags, wire dependencies, call exported setup.
Point dependencies toward the domain: cmd → application → infrastructure.
Use go list ./..., go mod tidy and go list -deps to inspect the graph.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.