Go organizes code in three layers: files group declarations, packages (one directory each) group related files, and modules (one go.mod) group packages into a versioned unit. Good structure means small, focused packages with clear names, dependencies that point in one direction, and the internal directory to keep implementation details private.
There is no mandatory project layout in Go. Start simple and add directories only when the code needs them.
A package is all .go files in one directory sharing the same package clause. Names starting with an uppercase letter are exported and visible to other packages. Lowercase names are private to the package.
Name packages with short, lowercase, single words such as auth, store or orders. Avoid utils, common and helpers, because they become dumping grounds and read badly at the call site (utils.Do says nothing). Do not repeat the package name in identifiers: orders.Service is better than orders.OrdersService.
A module is a tree of packages with a go.mod file at its root. Create one with go mod init.
The module path is the import prefix for every package in it. Use these commands to manage dependencies:
| Command | Purpose |
|---|---|
go get example.com/[email protected] | Add or change a dependency version |
go mod tidy | Add missing and remove unused requirements |
go list -m all | List all modules in the build |
go mod vendor | Copy dependencies into a vendor directory |
go work init ./a ./b | Use several local modules together |
Commit both go.mod and go.sum. Follow semantic versioning: a breaking change in v2 or later requires the major version in the module path, for example github.com/acme/shop/v2.
For a service or application, this layout is common and scales well:
cmd/<name>/main.go holds one main package per executable and stays thin.internal/ is enforced by the compiler: only code inside the parent of internal can import it. Put most of your code here so you can refactor without breaking users.pkg/ is a convention rather than a rule. Many projects skip it, and a small library can keep its files in the module root.A single-purpose command can be just go.mod and main.go. Do not create cmd, internal and pkg before you need them.
Keep dependencies pointing inward: business logic should not import the database or HTTP packages. Define small interfaces in the package that uses them, and let concrete types satisfy them implicitly.
In tests you pass a fake Store, so orders can be tested without a database. Wiring concrete types together in main keeps the rest of the program free of global state.
Go forbids circular imports, and the compiler reports import cycle not allowed. If package a needs b and b needs a, you have a design problem. Typical fixes:
| Scenario | Suggested structure |
|---|---|
| Small script or CLI | One main.go, then split files by topic in the same package |
| Library | Code in the module root, public API in the root package, details under internal/ |
| Web service | cmd/, internal/ with domain, storage and transport packages |
| Several services in one repo | One module per service with a go.work file, or a single module with several cmd/ entries |
| Shared code across modules | A separate versioned module, kept small and stable |
Keep test files next to the code they test, and put test data in a testdata directory, which the Go tool ignores when building.
No. The Go team documents modules and packages but does not mandate a directory structure. The cmd, internal layout is a widely used convention.
internal directory do?Packages under internal can be imported only by code rooted at the directory containing internal. The compiler enforces this, so external modules cannot depend on them.
No. Split a package into several files by topic when it grows, such as service.go, errors.go and store.go. A package is the unit of design, not the file.
Only when it needs its own version and release cycle or is consumed by other projects. Extra modules add dependency management overhead.
Organize Go code with small, well-named packages inside a module, keep the entry point in cmd, hide implementation details in internal, and define interfaces where they are used. Avoid cycles by layering dependencies, start with the simplest layout, and grow it only when the project demands it.