DEV Community

Quang Hieu (Bee)
Quang Hieu (Bee)

Posted on

Backend Project Structure Go

Image description

Hi everyone, today I want to share a Go project structure that I have used and found useful. This project structure is suitable for medium and small projects, and even large projects. The way folders and files are organized in this project structure is similar to other programming languages such as NodeJS or Java. This helps everyone to have a more comprehensive and easier understanding when learning about Go. I hope it will be useful and here is my Go project structure.

.
├── cmd/               # Contains executable applications
│   ├── cli/           # Command-line application
│   ├── cronjob/       # Scheduled jobs
│   └── server/        # Server application
│      └── main.go     # Run the application
├── config/            # Configuration for applications
│   └── config.yaml    # Main configuration file
├── docs/              # Project documentation
├── global/            # Global variables
├── internal/          # Internal packages
│   ├── controller/    # Handle client requests
│   ├── initialize/    # Initialize necessary components
│   ├── middlewares/   # Server middlewares
│   ├── models/        # Structs representing data
│   ├── repo/          # Query data from the database
│   ├── routers/       # Define routes for the server
│   ├── service/       # Handle business logic
├── migrations/        # Database migration scripts
├── pkg/               # Reusable packages
│   ├── logger/        # Logging for the application
│   ├── response/      # Handle response to the client
│   ├── setting/       # Application settings
│   └── utils/         # Utility functions
├── scripts/           # Development support scripts
├── tests/             # Test cases for the application
├── third_party/       # Third-party libraries
├── .gitignore         # Git ignore file
├── go.mod             # Go dependencies management
├── go.sum             # Contains checksums of dependencies
├── LICENSE            # Project license
└── README.md          # Project description
Enter fullscreen mode Exit fullscreen mode

This is the result of my project structure on Visual Studio Code

Image description

If you found this article useful and interesting, please share it with your friends and family. I hope you found it helpful. Thanks for reading 🙏

Let's get connected! You can find me on:

Top comments (3)

Collapse
 
b3pr0 profile image
b3pr0 • Edited
  • Instead of pkg and third_party just use vendor, vendor is a default name, which is used everywhere for the third-party packages.
  • What is internal? Entire this code is internal.
  • cmd, then cli... God, stop it please! These names mean the same.
  • cronjob? globals? locals?

This is a typical, bad-structured project in Go. This is a bad template, which is used only in Go shit stuff, mate. I don't blame you, but I do blame the people who provided that shit, mate.

Collapse
 
frulio profile image
Frulio

I guess the pkg folder if for reusable packages written by the author, but third_party doesn't make sense to me for the same reason that you mention.

Other than that I mostly agree with your comment, for me it seems a company policy made by someone that was learning Go at that moment.

Collapse
 
wanted_wilson_bbd50f96ec3 profile image
wanted wilson

where did you bring initialize from?! use infra instead