gitmyhub

go-clean-arch

Go ★ 10k updated 2y ago

Go (Golang) Clean Architecture based on Reading Uncle Bob's Clean Architecture

A Go codebase that demonstrates how to structure a backend application using Clean Architecture, separating business logic from databases and web frameworks so each layer can be tested and swapped independently.

GoMySQLDockersetup: moderatecomplexity 2/5

go-clean-arch is a Go codebase that demonstrates how to structure a backend application using Clean Architecture, a design philosophy popularized by software author Robert C. Martin (often called Uncle Bob). The goal of Clean Architecture is to keep the core business logic of an application isolated from the specific frameworks, databases, and delivery mechanisms it uses. That isolation makes the business logic easier to test and means you can swap out a database or a web server without rewriting the core code.

The project organizes code into four layers. The Models layer holds the data structures. The Repository layer defines how data is stored and retrieved. The Usecase layer contains the actual business rules, with no knowledge of how data gets to or from the outside world. The Delivery layer is what users and other systems interact with, such as an HTTP API. Each layer depends only on the layers below it, not above.

The repository has gone through four versions since 2017, each refining the directory structure and conventions. The current master branch (v4) introduces a few newer patterns: interfaces declared on the consuming side rather than the providing side, an internal package for code that should not be imported externally, and a service-focused package organization. Earlier versions are preserved on separate branches if you prefer a simpler layout.

To run the project locally, you clone the repository, copy the example environment file, and run docker-compose. A MySQL database is required, and a SQL file is included to set up the schema. The README describes this as a learning reference rather than a prescriptive template, and the author notes that different arrangements of the same core ideas are equally valid.

Where it fits