Go has no mandatory project structure, but the community has settled on a few conventions: put each executable under cmd/<name>, keep code that must not be imported by other modules under internal/, and optionally place reusable library code under pkg/. The Go team documents the go standard project layout as a guide, not a rule, and the best layout is the smallest one that fits your project.
This guide explains what each folder is for, shows a complete example, and covers the one folder name the compiler actually enforces.
The Go toolchain only cares about three things: a go.mod at the module root, one package per folder, and the special meaning of internal. Everything else is convention. A tiny tool can live as a single main.go plus go.mod. Start flat and add folders when the code grows; do not create empty cmd, pkg and internal folders on day one.
Each folder under cmd holds a main package that builds one binary. The folder name becomes the default binary name.
Keep main thin: parse flags and configuration, wire dependencies, start the program. The real logic lives in packages that tests can call.
A package inside a folder named internal can be imported only by code rooted in the parent of that internal folder. This is checked by the compiler, not by convention.
Code anywhere in example.com/shop/... can import example.com/shop/internal/server. Another module that tries it gets the error use of internal package example.com/shop/internal/server not allowed. You can also nest it: shop/billing/internal/tax is visible only inside shop/billing.
Use internal as the default home for application code. It lets you change those APIs freely, because no outside module can depend on them.
pkg/ is meant for code that is safe for other projects to import. Some well-known projects use it and many do not. It adds a path segment, and a module of mostly library code can place packages directly at the root instead.
| Folder | Purpose | Importable from other modules |
|---|---|---|
cmd/ | One subfolder per binary, thin main packages | Not meant to be |
internal/ | Private application and library code | No, enforced by the compiler |
pkg/ | Intentionally public library packages | Yes |
| Module root | Public package of a library, or small program | Yes |
Practical advice: if you ship an application, you probably need cmd and internal and no pkg. If you ship a library, put the public package at the root and hide helpers in internal.
Other common folders are testdata/ (ignored by the build, used by tests), migrations/ or db/ for SQL, api/ for OpenAPI or protobuf files, and scripts/ for helpers. Name packages after what they provide (store, config), not utils or common.
*_test.go.cmd imports internal, and internal/server imports internal/store, never the reverse.store.New, not store.NewStore.No. They are conventions. Use only those that solve a problem you have today.
Yes. For one binary, a root main.go is simple, and go install example.com/tool@latest installs it. Use cmd/ when you have several binaries or a library plus a command.
No, the compiler enforces it for imports from outside the parent tree.
Give it its own module with exported packages, and connect it with a go.work file during development.
Use cmd/<binary> for entry points, internal/ for private code the compiler protects, and pkg/ only when you deliberately publish library packages. Start simple, split into packages by responsibility, and keep main small so the rest of the program is easy to test.