Best Division

Spread Your Knowledge

  • All topics
  • About us
  • Contact us
  • Privacy policy
  • Terms of service
  • Copyright
  • Facebook
  • Twitter
  • Instagram
  • Youtube
  • LinkedIn
  • Pinterest
  • Github
  • Tumblr

© 2026 Best Division

Go Code Organization vs Modularization Explained

Go
Jayant Kumar
Jayant Kumar@jayantkumar314
PublishedMar 13, 2023
UpdatedNov 17, 2026
5 min read

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.

Table of Contents

  • Packages, modules and workspaces
  • Code organization: laying out the repository
  • Modularization: boundaries and visibility
  • Rules that keep modules healthy
  • Organization versus modularization side by side
  • Frequently Asked Questions
    • Do I need cmd, internal and pkg directories?
    • How big should a Go package be?
    • When should I create a second module?
    • Is a monorepo with many modules supported?
  • Summary

Packages, modules and workspaces

Three levels matter:

LevelWhat it isDefined by
PackageA directory of .go files compiled togetherpackage name line
ModuleA tree of packages versioned togethergo.mod at the root
WorkspaceSeveral local modules developed togethergo.work

Create a module with:

bash

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.

Code organization: laying out the repository

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:

text
  • 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.
  • A top-level package (for example 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.

Modularization: boundaries and visibility

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.

go
go

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.

Rules that keep modules healthy

  1. Name packages after what they provide (orders, billing), not what they contain (models, types).
  2. Avoid import cycles; the compiler rejects them, so move shared types to a lower-level package or use an interface.
  3. Define interfaces in the consumer package and keep them small.
  4. Hide implementation details behind internal/ and unexported names.
  5. Keep main thin and put logic in testable packages.
  6. Tag releases with semantic versions (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.

Organization versus modularization side by side

AspectCode organizationModularization
Question answeredWhere does this file go?What does this unit expose and depend on?
Main toolsDirectories, file names, namingPackages, modules, internal, interfaces
Enforced byConvention and reviewThe compiler and go.mod
BenefitEasy navigationLow coupling, reuse, independent versioning

Frequently Asked Questions

Do I need cmd, internal and pkg directories?

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.

How big should a Go package be?

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 should I create a second module?

When a part needs its own version history or release cycle, or other teams import it separately. Otherwise one module per repository is simpler.

Is a monorepo with many modules supported?

Yes. Use go.work locally and replace or published versions elsewhere.

Summary

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.

Share