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

How do you structure and organize code in Go projects?

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

Organise a Go project around Go modules and packages that each have one clear responsibility. Start with the simplest layout that works, put executables under cmd/, keep private code in internal/, and add directories only when the project needs them. Go has no mandatory layout, and the official guidance favours simplicity over a fixed template.

The recommendations below apply to current Go releases and to both small services and large multi-binary repositories.

Table of Contents

  • Start simple and grow the layout
  • Use Go modules
  • Design packages around responsibilities
  • Follow Go naming conventions
  • Place tests, configuration and tooling
  • Frequently Asked Questions
    • Is the golang-standards project-layout the official layout?
    • When should I use the internal directory?
    • Should I put everything in one package?
    • Where do I put shared helpers?
  • Summary

Start simple and grow the layout

For a small program or library, a flat layout is enough:

text

As the project grows, introduce structure. A typical service looks like this:

text

Key points:

  • cmd/<name>/main.go holds one executable each. Keep main thin, since it should only parse configuration, build dependencies and start the program.
  • internal/ is enforced by the Go toolchain: packages inside cannot be imported from outside the parent module tree. Put most application code here so you can refactor freely.
  • pkg/ is a convention, not a rule. Many projects skip it. Use it only if you intend external projects to import those packages, and remember that every exported package becomes a compatibility promise.
  • Do not put a tests/ directory of unit tests apart from the code. Go tests live next to the code they test.

Use Go modules

A module is defined by a go.mod file at the repository root. These are shell commands:

bash
  • Commit go.mod and go.sum.
  • Module paths should match where the code is hosted, and a major version of 2 or higher adds a /v2 suffix to the path.
  • Run go mod tidy before committing to remove unused dependencies. Use go get -u ./... carefully, and read the changes.
  • For several related modules in one repository, use a go.work workspace file for local development.

Design packages around responsibilities

Package design matters more than directory names.

  1. Name packages after what they provide, in short lower-case singular words (order, auth), not generic names such as util, common or helpers.
  2. Give each package a single purpose, and keep its exported surface small.
  3. Avoid import cycles by depending in one direction: handlers depend on services, services depend on storage interfaces, and domain types depend on nothing.
  4. Define interfaces where they are consumed, not where they are implemented.
  5. Split a package when it grows several unrelated responsibilities, not just because it has many files.
go

Because Service depends on a small interface, you can test it with an in-memory fake and swap the database implementation without touching business rules.

Follow Go naming conventions

  • Exported identifiers start with an upper-case letter, and unexported identifiers with lower case.
  • Use MixedCaps, not snake_case. Acronyms keep one case: HTTPClient, userID.
  • Avoid stutter: in package order, name the type Order, not OrderOrder, and the constructor order.New.
  • Keep short names for short scopes, such as i, r for a reader or ctx for context, and descriptive names for wider scopes.
  • Name getters without a Get prefix (user.Name()), and one-method interfaces with an -er suffix (Reader, Closer).
go

Place tests, configuration and tooling

Unit tests live in the same directory with a _test.go suffix. Use the same package name to test internals, or the _test package suffix to test only the public API.

go

Run all tests and checks from the repository root:

bash

For configuration, read environment variables or a file in main, build a typed config struct, and pass it down, rather than reading globals deep inside packages. Keep build scripts, Dockerfiles and CI configuration in predictable top-level places, and use //go:embed for static files that must ship inside the binary.

Frequently Asked Questions

Is the golang-standards project-layout the official layout?

No. That repository is a community convention, not an official Go standard. The Go team recommends starting simple, as described in the official guide on organising a Go module.

When should I use the internal directory?

Use it for code that should not be imported by other modules. It lets you change implementation details without breaking users.

Should I put everything in one package?

For small programs, yes. Split when a package has distinct responsibilities or becomes hard to navigate, and not before.

Where do I put shared helpers?

Place them in the package that uses them most, or create a focused package with a specific name. Avoid catch-all utils packages.

Summary

Keep Go project structure as simple as the project allows. Use a module per repository, cmd/ for executables, internal/ for private code and small, responsibility-focused packages with consumer-defined interfaces. Follow Go naming conventions, keep tests beside the code, and let the toolchain (go vet, go test -race, go mod tidy) enforce quality as the project grows.

Share

Similar posts

  • How Do You Create a Go Module with go mod init?
  • When Should You Use panic Instead of Returning an Error?
  • How Do You Combine Multiple Errors with errors.Join?
  • How Do You Use errors.Is and errors.As in Go?
  • What Are Sentinel Errors in Go?
  • What Does Accept Interfaces, Return Structs Mean in Go?
  • What Are Stringer, Reader and Writer Interfaces in Go?
  • Why Is an Interface Holding a Nil Pointer Not Nil in Go?