Starting a journey with a new programming language is always challenging — and I've been there myself.
I began learning Go (Golang) when I was comparing it with Python for some tasks deployed on AWS Lambda. The cold start time with Python was too high, so I started looking for alternatives. That’s when I discovered Golang.
After learning the basics, the first big question I had was:
- What is the best way to structure a Go project?
- How do companies organize and maintain Golang repositories?
Key Learnings About Go Project Setup
- Every Go file must start with a package name.
- The package name doesn't need to match the file or folder name.
- A project's go version and module is defined in the go.mod file, which should always be in the root directory.
- The go.sum file is auto-generated when you run
go mod tidy
. It records dependency versions to ensure reproducible builds.
In the end, you'll have a standalone executable binary file that can be published on the Go package manager or deployed (for example, on AWS Lambda).
Clean Go Project Structure (Template)
When building maintainable applications, following a clean and well-organized folder structure is essential. Here's a recommended Golang project structure you can use as a template:
root
|
|- build/ # Packaging and CI/CD related files
|- cmd/ # Application entry point
| └── main.go # For single entry point
| └── EntryPointA # For multiple entry points
| └── *.go
| └── EntryPointB # For multiple entry points
| └── *.go
|- internal/ # Private application and library code(internal logic)
| └── *.go # write your business logic also we can split them into packages like api, database, auth, etc
| └── packageA # packageA code
| └── *.go
| └── packageB # packageB code
| └── *.go
|- pkg/ # Public utility functions and reusable code
| └── *.go
| └── util # Add Util code like validator, generic code, etc.
| └── *.go
|- scripts/ # Automation and helper scripts
|- configs/ # Configuration files (YAML, JSON, TOML, etc.)
|- go.mod # Module definition
|- go.sum # Dependency lock file (auto-generated)
This folder layout is inspired by clean architecture principles and helps keep Go projects scalable, testable, and easier to collaborate on.
Why Follow This Folder Structure?
- Improves readability and maintainability
- Supports clean architecture and modular code
- Makes it easy for new developers to understand the project
- Ensures scalable backend development with Go
Conclusion
If you're just starting with Golang, this template will give you a solid foundation for structuring your projects. Following a clean folder structure and using Go best practices ensures your applications are easier to maintain and scale.
💬 What do you think of this Go project structure? Did I miss something important? Share your thoughts in the comments — I'd love to learn from your experience too!
Top comments (0)