Skip to content

Instantly share code, notes, and snippets.

@ayoubzulfiqar
Created September 5, 2023 06:12
Show Gist options
  • Save ayoubzulfiqar/9f1a34049332711fddd4d4b2bfd46096 to your computer and use it in GitHub Desktop.
Save ayoubzulfiqar/9f1a34049332711fddd4d4b2bfd46096 to your computer and use it in GitHub Desktop.
The Folder Structure for Every Golang Project

Go - The Ultimate Folder Structure

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.

Other Possible

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:

  1. 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/
  2. 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/
  3. 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/
  4. 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/
  5. 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.

@iamSuva
Copy link

iamSuva commented Jul 8, 2024

It is a great explanation.Thank you for sharing

@dpi0
Copy link

dpi0 commented Aug 26, 2024

extremely useful!

@imanborumand
Copy link

thank you

@Shobhit-Nagpal
Copy link

oh dude, this is great!

@dangquyitt
Copy link

the case of DDD, how can we avoid circular imports when auth needs to import order and vice versa?

@ayoubzulfiqar
Copy link
Author

the case of DDD, how can we avoid circular imports when auth needs to import order and vice versa?

In Domain-Driven Design (DDD), circular dependencies between modules (like auth and order) can arise when different domains need to access each other's functionality. This is often a sign that the boundaries between the domains are not well defined or that one of the domains has too much coupling with the other.

Some Stratgies that i try if you really curious:

  1. Use Interfaces to Decouple
  2. Use Dependency Injection
  3. Event-Driven Approach (Event Sourcing or Domain Events)

and if you really wanna know more you can look into other principles that might be somthing that you are looking for

@rafaelfnaves
Copy link

This is great! Thanks.

@danielnicolae28
Copy link

The best explanation! Thank you!

@toluhikay
Copy link

Hey dude I love this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment