How to structure a Go project is a question with a simple answer at the top and a lot of detail underneath: one module, packages by purpose, and a layout that grows with the code instead of fighting it. Go’s tooling makes most of the decisions for you. The module file names the project, the package system groups related files, and a few conventions cover the rest. This guide shows the layout that works for small tools and large services alike, and explains why each piece sits where it does.
A good layout pays off when the project grows. The code you write in week one is easy to find in month six only if the structure says where things live. Spend the effort now, and the project stays navigable when it has fifty files instead of five.
Modules: One Project, One go.mod
A Go module is a collection of packages versioned together, and go.mod is the file that declares it. Create it with:
mkdir myapp && cd myapp
go mod init example.com/myapp
go mod tidy
The module path, here example.com/myapp, becomes the prefix for every import inside the project. A package in the folder internal/auth is imported as example.com/myapp/internal/auth. That full path looks long, but it removes all ambiguity about where a package comes from, and it is the reason Go projects never need a separate dependency map for their own code.
go mod tidy reads your imports and writes the exact dependency versions into go.mod and go.sum. Commit both files. The sum file records hashes, so a build on another machine verifies it downloads the same code. Never edit go.sum by hand, and never leave go.mod out of version control.
If you come from JavaScript, the module idea maps to a package.json with workspaces, but Go enforces it more strictly. The comparison with JavaScript tooling is worth a look in the guide on TypeScript vs JavaScript in 2026, which shows how the JavaScript side handles the same versioning problem with a different set of tools.
How to Structure a Go Project: Packages by Purpose
A package is a folder of .go files that share a package name. Go’s rule is simple: group files by what they do, not by what they are. A package called user that holds the user model, its storage, and its validation is better than three packages called models, storage, and validators that all change together.
The standard library sets the example. Packages like net/http and encoding/json are small, focused, and named after the thing they do. Follow that: short lowercase names, no underscores, no stuttering. A package inside user should not contain a type called UserUser. Keep exported names, the ones starting with a capital letter, few and deliberate, because every exported name is part of the package’s public surface.
One more rule matters: a package should not import a package that imports it back. Go refuses circular imports at compile time. When you hit one, it usually means two packages are really one package, or a shared piece belongs in a third package both can import. Split the shared piece out instead of fighting the compiler.
The Layout That Scales
For a service of any real size, this layout works well:
myapp/
cmd/
api/
main.go
internal/
auth/
user/
store/
pkg/
httpx/
go.mod
go.sum
cmd/api/main.go holds the entry point: it reads config, builds dependencies, and starts the server. It should be thin, mostly wiring, because logic in main is hard to test. internal/ holds packages only this module can import. Go enforces that: code outside the module cannot import anything under internal. Put your business logic there. pkg/ holds packages other projects could reuse, such as a small HTTP helper. Use it sparingly. Most code belongs in internal, and a pkg folder full of speculative reuse is a smell.
Small tools do not need all of this. A single main package with a few files is fine for a CLI with one job. Add internal when a second concern appears, and add cmd when a second binary appears. The layout should follow the project, not the other way around.
Where Config, Tests, and Data Live
Config belongs in one place, loaded once at startup and passed down as a value. Environment variables are the usual source, read in main and handed to the packages that need them. A package that reads environment variables directly is hard to test, because the test has to set global state first. Keep secrets out of the code entirely, and the guide on how developers should store secrets shows the options for managing them properly once a project leaves your laptop.
Tests live next to the code they test, in files ending with _test.go. A test file in the same package can reach unexported functions, which makes testing internals easy. Run them with go test ./... from the module root, and add -race when concurrency is involved. Test data, fixtures, and example files go in a testdata folder, which Go tooling ignores during builds.
Static assets, templates, and migration files each get their own folder at the root or inside the package that uses them. The rule is the same as for code: the folder name should say what the files are for, and the package that needs them should be the one that owns them.
Common Layout Mistakes
The most common mistake is copying a layout built for a giant project into a small one. A service with three endpoints does not need cmd, internal, pkg, and api folders. It needs a main file and a package or two. Over-structuring early makes every change touch five folders.
The second mistake is the opposite: one package with forty files. When a single package holds unrelated concerns, finding code becomes a search exercise and unrelated changes collide. Split when a group of files changes together for one reason and the rest changes for another.
The third mistake is deep nesting. Go packages work best flat, with a handful of folders at most two levels deep. If a path reads like internal/domain/user/service/handler, the structure is describing an org chart instead of the code. Flatten it.
Deployment is where layout meets reality: a single Go binary with no runtime to install is easy to ship, and the walkthrough on how to set up a staging environment on a separate VPS shows how to test that binary in a place that mirrors production before it goes live.
Keep the Structure Honest
A good Go layout is boring: modules at the root, packages by purpose, tests beside code, and folders that appear only when the code needs them. Resist the urge to design the perfect tree up front. Start flat, split when a package gets crowded, and move code when its purpose changes. The compiler will tell you when an import cycle means a split was wrong, and go vet will catch the small mistakes along the way. Structure is a tool for finding code quickly, and the simplest layout that does that job is the right one.