Go Project Structure
A scalable and maintainable folder structure for Go applications
File Structure
- cmd
- api
- main.go
- worker
- main.go
- internal
- api
- handlers
- user.go
- auth.go
- middleware
- auth.go
- cors.go
- routes
- routes.go
- domain
- user
- model.go
- repository.go
- service.go
- infrastructure
- database
- postgres.go
- migrations
- cache
- redis.go
- config
- config.go
- database.go
- pkg
- logger
- logger.go
- validator
- validator.go
- api
- openapi.yaml
- deployments
- docker
- kubernetes
- scripts
- build.sh
- test.sh
- test
- integration
- fixtures
- go.mod
- go.sum
- Dockerfile
- Makefile
- .gitignore
- README.md
Directory Structure Explanation
cmd/api/
API server application entry point for the REST API service.
cmd/worker/
Background worker application for processing jobs and tasks.
internal/api/
API-specific code including handlers, middleware, and routing logic.
internal/api/middleware/
HTTP middleware for authentication, CORS, logging, and other cross-cutting concerns.
internal/domain/
Domain-driven design structure with business logic organized by domain entities.
internal/domain/user/
User domain containing model, repository interface, and service logic.
internal/infrastructure/
Infrastructure layer containing database, cache, and external service implementations.
infrastructure/database/
Database connection, repository implementations, and migration files.
pkg/logger/
Reusable logging package that can be used across different applications.
api/
API documentation and specifications including OpenAPI/Swagger files.
deployments/
Deployment configurations for Docker, Kubernetes, and other platforms.
test/
Additional test files including integration tests and test fixtures.
Makefile
Build automation with common tasks like build, test, and deploy.