Sefism is free for learners. We are here to help you grow, one step at a time.Join for free
Sefism.

Project structure · Backend

Go REST API project structure

Opens with credits

A JSON API in Go using the standard library router, PostgreSQL through pgx and sqlc, SQL migrations with goose, and tests next to the code. It follows the layout the Go team itself documents: commands in cmd/, everything else in internal/, one package per feature plus a small platform package for the plumbing, and nothing added until it is needed.

Curated by Tauseef FayyazIntermediate176 files and foldersGo 1.27, net/http ServeMux patterns, log/slogChecked October 2026
Explore the filesPick any file to see what it is, why it exists and when you add it.

How it grows

Each stage opens the explorer there

  1. 1Day 1What the scaffold and your first hour give you13 files and folders
  2. 2GrowingWhat a real app with features and tests adds+73 files and folders
  3. ProductionWhat running it for real users adds+43 files and foldersOpens with credits
  4. ExpertWhat a team needs to keep it healthy for years+47 files and foldersOpens with credits

How it is organised

  • Start flat and add folders when they earn their place. The Go team says a small project can be a single main package; this tree is what a growing API needs over years, not a starting template to copy whole.
  • Commands live in cmd/<name>/main.go and stay tiny. All logic lives in internal/, which the Go toolchain refuses to let other modules import.
  • Skip pkg/. The official layout guide never mentions it, and for a server nothing is meant to be imported by others. The popular golang-standards/project-layout repository is a community collection, not a Go standard, and the Go team has said so.
  • Package by feature (internal/order), not by layer (internal/handlers, internal/models). Each feature owns its handler, service, store and SQL. Plumbing every feature shares (config, logging, database, middleware) lives in internal/platform.
  • Dependencies point one way: server imports features, features import platform, platform imports nothing of yours. Go rejects import cycles at compile time, and an architecture test keeps the rest honest.
  • Define interfaces where they are used, small, and only when there is a second implementation or a test needs one. Accept interfaces, return structs.
  • Tests sit next to the code as _test.go files in the same package. Only black-box suites that run the whole binary (e2e, contract, load) get a separate test/ folder.
  • Pass context.Context through every call that does I/O, log with log/slog in JSON, and never let a goroutine outlive the request or the server that started it.

At the top of the project

Open one in the explorer for the full story

Learn more on Sefism

Roadmaps, cheat sheets and projects that go with Go REST API.

See 10 more
Checked against 19 official sources, October 2026. If something has changed, the docs win.Show sources

Picked from the other libraries by shared topics.

Work with me

Stuck on something specific?

Writing only gets you so far. If you want an answer to your situation rather than the general case, book a session and we will work through it together. Sessions are free for approved Sefism members, and a few slots open each week.

  • 1:1 Mentorship
  • Fresh Graduate Mentorship
  • Career Guidance

Brand deals, promos and partnerships: tauseeffayyaz@gmail.com