In Go, code organization is how you arrange files, directories and packages inside a repository, while modularization is how you split code into units with clear boundaries that can be versioned and reused, mainly through packages and Go modules. Organization decides where things live; modularization decides what depends on what and what is exposed. Good Go projects use both: small, focused packages inside one or more modules, with an explicit public API.
Go gives you only a few tools for this: packages, the internal directory, exported names and modules. They are enough when used with discipline.
Three levels matter:
| Level | What it is | Defined by |
|---|---|---|
| Package | A directory of .go files compiled together | package name line |
| Module | A tree of packages versioned together | go.mod at the root |
| Workspace | Several local modules developed together | go.work |
Create a module with:
Everything below that directory is imported with that path prefix, for example github.com/acme/shop/internal/orders. Use go work init ./api ./worker when you are editing several modules at once, and do not commit a go.work file unless your team agrees on it.
Go has no mandatory layout. The official guidance is to start simple and add structure when you need it. A small program can be one main.go. A typical service looks like this:
cmd/<name>/main.go holds one entry point per binary and stays thin.internal/ holds code that only this module may import. The compiler enforces it.shop itself) is the right home for reusable library code that others import.The pkg/ directory is a popular convention, but it is not an official standard and adds a path segment with no benefit for most projects. Prefer clear package names over generic ones such as utils, common or helpers.
Visibility in Go is decided by capitalisation. A name starting with an uppercase letter is exported; lowercase names are private to the package. Combine this with internal/ to keep your public surface small.
The orders package depends on an interface, not on PostgreSQL. main wires the concrete pieces together. This direction of dependency is the heart of modular Go code.
orders, billing), not what they contain (models, types).internal/ and unexported names.main thin and put logic in testable packages.v1.4.0). For major version 2 and above, the module path ends in /v2.Use go vet ./..., go list -deps, and go mod tidy to keep the dependency graph clean.
| Aspect | Code organization | Modularization |
|---|---|---|
| Question answered | Where does this file go? | What does this unit expose and depend on? |
| Main tools | Directories, file names, naming | Packages, modules, internal, interfaces |
| Enforced by | Convention and review | The compiler and go.mod |
| Benefit | Easy navigation | Low coupling, reuse, independent versioning |
No. Start with a flat layout. Add cmd/ when you have more than one binary and internal/ when you need to prevent outside imports. pkg/ is optional and often unnecessary.
Big enough to be cohesive, small enough to describe in one sentence. Splitting by technical layer (all handlers, all models) usually creates cycles; splitting by feature works better.
When a part needs its own version history or release cycle, or other teams import it separately. Otherwise one module per repository is simpler.
Yes. Use go.work locally and replace or published versions elsewhere.
Code organization arranges files and directories, and modularization defines package boundaries, visibility and dependencies. In Go, use packages named after features, internal/ to hide code, small consumer-side interfaces, and one module per releasable unit. Keep the layout as simple as the project allows.