Organizing your Go (Golang) project's folder structure can help improve code readability, maintainability, and scalability. While there is no one-size-fits-all structure, here's a common folder structure for a Go project:
project-root/
βββ cmd/
β βββ your-app-name/
β β βββ main.go # Application entry point
β β βββ ... # Other application-specific files
β βββ another-app/
β βββ main.go # Another application entry point
β βββ ...
βββ internal/ # Private application and package code
β βββ config/
β β βββ config.go # Configuration logic
β β βββ ...
β βββ database/
β β βββ database.go # Database setup and access
β β βββ ...
β βββ ...
βββ pkg/ # Public, reusable packages
β βββ mypackage/
β β βββ mypackage.go # Public package code
β β βββ ...
β βββ ...
βββ api/ # API-related code (e.g., REST or gRPC)
β βββ handler/
β β βββ handler.go # HTTP request handlers
β β βββ ...
β βββ middleware/
β β βββ middleware.go # Middleware for HTTP requests
β β βββ ...
β βββ ...
βββ web/ # Front-end web application assets
β βββ static/
β β βββ css/
β β βββ js/
β β βββ ...
β βββ templates/
β βββ index.html
β βββ ...
βββ scripts/ # Build, deployment, and maintenance scripts
β βββ build.sh
β βββ deploy.sh
β βββ ...
βββ configs/ # Configuration files for different environments
β βββ development.yaml
β βββ production.yaml
β βββ ...
βββ tests/ # Unit and integration tests
β βββ unit/
β β βββ ...
β βββ integration/
β βββ ...
βββ docs/ # Project documentation
βββ .gitignore # Gitignore file
βββ go.mod # Go module file
βββ go.sum # Go module dependencies file
βββ README.md # Project README
Here's a brief explanation of the key directories:
-
cmd/
: This directory contains application-specific entry points (usually one per application or service). It's where you start your application. -
internal/
: This directory holds private application and package code. Code in this directory is not meant to be used by other projects. It's a way to enforce access control within your project. -
pkg/
: This directory contains public, reusable packages that can be used by other projects. Code in this directory is meant to be imported by external projects. -
api/
: This directory typically holds HTTP or RPC API-related code, including request handlers and middleware. -
web/
: If your project includes a front-end web application, this is where you'd put your assets (CSS, JavaScript, templates, etc.). -
scripts/
: Contains scripts for building, deploying, or maintaining the project. -
configs/
: Configuration files for different environments (e.g., development, production) reside here. -
tests/
: Holds unit and integration tests for your code. -
docs/
: Project documentation, such as design documents or API documentation.
This is a general guideline, and you can adjust it to match the specific needs of your project. Additionally, you can consider using a project layout tool like "golang-standards/project-layout" as a reference to structure your Go project.
The folder structure for a Go project can vary depending on the size and complexity of the project, as well as personal or team preferences. Here are some alternative folder structures for Go projects:
-
Flat Structure: In smaller projects, you might opt for a flat structure where all your Go source files reside in the project root directory. This approach is simple but may become hard to manage as the project grows.
project-root/ βββ main.go βββ handler.go βββ config.go βββ database.go βββ ... βββ static/ βββ templates/ βββ scripts/ βββ configs/ βββ tests/ βββ docs/
-
Layered Structure: Organize your code into layers, such as "web," "api," and "data." This approach helps separate concerns.
project-root/ βββ main.go βββ web/ β βββ handler.go β βββ static/ β βββ templates/ βββ api/ β βββ routes.go β βββ middleware/ βββ data/ β βββ database.go β βββ repository.go βββ configs/ βββ tests/ βββ docs/
-
Domain-Driven Design (DDD): In larger applications, consider structuring your project based on domain-driven design principles. Each domain has its own directory.
project-root/ βββ cmd/ β βββ app1/ β βββ app2/ βββ internal/ β βββ auth/ β β βββ handler.go β β βββ service.go β βββ orders/ β β βββ handler.go β β βββ service.go β βββ ... βββ pkg/ β βββ utility/ β β βββ ... β βββ ... βββ api/ β βββ app1/ β β βββ ... β βββ app2/ β β βββ ... βββ web/ β βββ app1/ β β βββ ... β βββ app2/ β β βββ ... βββ scripts/ βββ configs/ βββ tests/ βββ docs/
-
Clean Architecture: You can adopt a clean architecture approach, which emphasizes a separation of concerns between different layers of your application.
project-root/ βββ cmd/ β βββ your-app/ β β βββ main.go βββ internal/ β βββ app/ β β βββ handler.go β β βββ service.go β βββ domain/ β β βββ model.go β β βββ repository.go βββ pkg/ β βββ utility/ β β βββ ... βββ api/ β βββ ... βββ web/ β βββ ... βββ scripts/ βββ configs/ βββ tests/ βββ docs/
-
Modular Structure: Organize your code into separate modules, each with its own directory structure. This approach can be useful when developing multiple independent components within a single project.
project-root/ βββ module1/ β βββ cmd/ β βββ internal/ β βββ pkg/ β βββ api/ β βββ web/ β βββ scripts/ β βββ configs/ β βββ tests/ β βββ docs/ βββ module2/ β βββ ...
Remember that the right folder structure depends on the specific needs of your project and your team's development practices. Choose a structure that helps maintain code organization, readability, and collaboration as your project evolves.
It is a great explanation.Thank you for sharing