Here's a well-structured Go project layout with explanations for each folder:
my-go-project/
├── cmd/
│ ├── server/
│ │ └── main.go
│ ├── cli/
│ │ └── main.go
│ └── worker/
│ └── main.go
├── internal/
│ ├── auth/
│ ├── user/
│ ├── order/
│ ├── config/
│ └── database/
├── pkg/
│ ├── logger/
│ ├── validator/
│ ├── httputil/
│ └── timeutil/
├── api/
│ ├── v1/
│ │ ├── handlers/
│ │ ├── middleware/
│ │ └── routes/
│ └── openapi/
├── models/
│ ├── user.go
│ ├── order.go
│ └── common.go
├── migrations/
├── scripts/
├── configs/
├── docs/
├── test/
├── go.mod
├── go.sum
├── Makefile
├── Dockerfile
└── README.md
Folder Explanations:
cmd/ - Contains the main applications for your project. Each subdirectory represents a different executable binary. The main.go files here should be minimal and primarily handle application initialization and dependency injection.
internal/ - Houses private application code that shouldn't be imported by other applications or libraries. This enforces Go's visibility rules where packages under internal/ can only be imported by code in the parent directory tree. Contains business logic, services, repositories, and application-specific utilities.
pkg/ - Contains library code that can be used by external applications. These are utilities and packages that could potentially be extracted into separate libraries. Code here should be well-documented and have stable APIs since it might be imported by other projects.
api/ - Contains API-related code including HTTP handlers, middleware, routing logic, and API documentation. Organize by API version (v1, v2) to support versioning. This is where your REST endpoints, GraphQL resolvers, or gRPC services would live.
models/ - Defines data structures, domain entities, and data transfer objects (DTOs). These represent your application's core data types and business entities. Keep database models, request/response structures, and domain objects here.
migrations/ - Database migration files for schema changes. Each migration should be versioned and include both up and down migrations.
scripts/ - Build scripts, deployment scripts, and other automation tools. Include database seeding scripts, CI/CD helpers, and development utilities.
configs/ - Configuration files for different environments (development, staging, production). Store YAML, JSON, or TOML configuration files here.
docs/ - Project documentation, API specifications, architecture diagrams, and user guides.
test/ - Integration tests, test fixtures, and test utilities that don't belong with unit tests in individual packages.
This structure follows Go community conventions and scales well as your project grows. The separation between internal/ and pkg/ helps maintain clear boundaries between private implementation details and reusable components.