Go supports modularity through packages, modules, small interfaces, composition by embedding, and generics. Write reusable code by giving each package one clear purpose, exposing a minimal API, accepting interfaces and returning concrete types, and versioning the whole thing as a module with go.mod.
Go deliberately has no inheritance or classes. Reuse comes from composition and from the tooling that makes sharing code between projects simple.
A package is a directory of .go files that share one package name. Anything that starts with an uppercase letter is exported; everything else is private to the package. That single rule is Go's whole visibility system, so choose names carefully.
Guidelines for good packages:
pricing or orders, not utils or common.orders.Service, not orders.OrdersService.internal/. The compiler enforces that only code inside the parent of internal can import it.A module is a tree of packages versioned together, defined by a go.mod file at its root.
Key points:
/v2, so old and new APIs can coexist.go.sum records checksums so downloads are verified. Commit both go.mod and go.sum.go mod tidy adds missing and removes unused requirements.go.work file (go work init ./a ./b) lets you develop several modules together without editing replace lines. Do not commit it if it contains local paths.go mod vendor copies dependencies into the repository when you need offline or reproducible builds.Interfaces in Go are satisfied implicitly. A type implements an interface just by having the methods, with no implements keyword, so the consumer of a behaviour can define the interface it needs.
Any email, SMS or fake test type with a matching Send method works. The proverb "accept interfaces, return structs" captures the practice: take the smallest interface as a parameter and return a concrete type so callers keep full access. Do not define an interface before there are at least two implementations or a testing need.
Embedding gives you code reuse without inheritance. The outer type gains the methods of the embedded type.
Use embedding for genuine "has the behaviour of" cases. For anything else, prefer a named field so the dependency is explicit and does not leak methods into your public API.
Since Go 1.18, generics let you write functions and types that work over many types while staying type-safe. Since Go 1.21 the standard library ships slices, maps and cmp, so check there before writing your own helpers.
Use generics for containers and algorithms that are genuinely independent of the element type. For behaviour that varies, an interface is still usually clearer.
Reusable constructors should stay stable as they grow. The functional options pattern lets you add settings without breaking callers:
| Practice | Why it helps |
|---|---|
| One purpose per package | Easier to understand, test and reuse |
| Small interfaces defined by the consumer | Loose coupling and easy fakes |
Use internal/ for private code | Compiler-enforced boundaries |
| Pass dependencies in constructors | No globals, simple testing |
| Document every exported name | go doc and pkg.go.dev render it |
Run go vet, gofmt and tests in CI | Consistent, safe shared code |
Write table-driven tests and add runnable Example functions to exported packages. The examples are compiled, executed by go test and shown in documentation.
No. Go uses composition through struct embedding and polymorphism through interfaces.
When it has its own release cycle or is reused by several projects. Otherwise, separate packages inside one module are simpler to maintain.
Usually in the consumer, which declares only the methods it needs. This keeps interfaces small and avoids forcing implementers to import a shared package.
No. Use them for type-independent containers and algorithms, and prefer plain functions or interfaces when the code is not meaningfully generic.
Go modularity rests on small packages with clear names, internal/ boundaries, modules versioned through go.mod, consumer-defined interfaces, composition by embedding, and generics where they remove duplication. Keep APIs small, pass dependencies explicitly, and document and test what you export.