go-clean-arch README

The project's README from github.com/bxcodec/go-clean-arch. As one markdown file: README.md.

go-clean-arch

Changelog

Author's Note

You may notice it diverges from the structures seen in previous versions. I encourage you to explore the branches for each version to select the structure that appeals to you the most. In my recent projects, the code structure has progressed to version 4. However, I do not strictly advocate for one version over another. You may encounter alternative examples on the internet that align more closely with your preferences. Rest assured, the foundational concept will remain consistent or at least bear resemblance. The differences are primarily in the arrangement of directories or the integration of advanced tools directly into the setup.

Description

This is an example of implementation of Clean Architecture in Go (Golang) projects.

Rule of Clean Architecture by Uncle Bob

More at https://8thlight.com/blog/uncle-bob/2012/08/13/the-clean-architecture.html

This project has 4 Domain layer :

The diagram:

golang clean architecture

The original explanation about this project's structure can read from this medium's post : https://medium.com/@imantumorang/golang-clean-archithecture-efd6d7c43047. It may be different already, but the concept still the same in application level, also you can see the change log from v1 to current version in Master.

How To Run This Project

Make Sure you have run the article.sql in your mysql

Since the project is already use Go Module, I recommend to put the source code in any folder but GOPATH.

Run the Testing

$ make tests

Run the Applications

Here is the steps to run it with docker-compose

#move to directory
$ cd workspace

# Clone into your workspace
$ git clone https://github.com/bxcodec/go-clean-arch.git

#move to project
$ cd go-clean-arch

# copy the example.env to .env
$ cp example.env .env

# Run the application
$ make up

# The hot reload will running

# Execute the call in another terminal
$ curl localhost:9090/articles

Tools Used:

In this project, I use some tools listed below. But you can use any similar library that have the same purposes. But, well, different library will have different implementation type. Just be creative and use anything that you really need.

go-clean-arch (bxcodec/go-clean-arch)

Searching Database…

这是什么?Uncle Bob 的整洁架构(Clean Architecture)在 Go 语言下的参考实现——一个示例项目,演示如何把真实的后端组织成分层、不依赖具体框架的代码。
它是一个可以导入的框架或库吗?都不是——这是一个完整的示例项目,你可以克隆下来阅读并把它当作模板改造成自己的服务,而不是一个加进 go.mod 的依赖包。
怎么把它跑起来?先把 article.sql 导入 MySQL 数据库;因为它使用 Go modules,源码要放在 GOPATH 之外;然后直接运行,或者按 README 里的 docker-compose 步骤操作。
我需要提前装好哪些东西?必须有 Go 和一个 MySQL 实例;也提供了用 Docker 配合 docker-compose 来运行的替代方式;测试里用到的 mock 由 mockery 生成。
master 分支上的结构就是我该照搬的结构吗?master 分支是 v4,它的目录布局和 v1–v3 不一样,而且作者本人保持中立——他建议把每个版本的分支都看一遍,选一个你最喜欢的结构。
这个项目是用来学习的,还是给生产环境用的?用于学习和作为起步参考——代码本身就是成果,完整的架构讲解可以看 README 里链接的作者 Medium 文章。
这四层分别是什么?Models、Repository、Usecase 和 Delivery。这样拆分的意义在于:业务规则保持独立,不依赖框架、UI、数据库,也不依赖任何外部因素。
它会不会把我绑死在 MySQL 上?不会——数据库无关性是核心原则之一。MySQL 只是这个示例所接的数据库,思路是你可以随时换成 Mongo、CouchDB 或其他任何数据库,而完全不用改业务规则。
这个示例应用到底能做什么?它是一个文章服务的演示:article.sql 用来初始化 MySQL 的表结构,请求会依次穿过这四层来完成文章的增删改查。
Creator
bxcodec
Size
0.4 MB
Stars
10.2K
Age
9y
Commits · 90 Days