go-clean-arch

基于阅读鲍勃大叔的《清晰架构》的 Go(Golang)清晰架构。「Go (Golang) Clean Architecture based on Reading Uncle Bob's Clean Architecture」

Github stars Tracking Chart

go-clean-arch

变更日志

说明

这是一个在 Go(Golang)项目中实现 Clean Architecture 的例子。

鲍勃大叔的清晰架构规则:

  • 独立于框架。该架构不依赖于某些功能丰富的软件库的存在。这允许您将这些框架用作工具,而不是将您的系统塞进它们有限的限制中。
  • 可测试性。业务规则可以在没有 UI、数据库、Web 服务器或任何其他外部元素的情况下进行测试。
  • 独立于 UI。用户界面可以很容易地改变,而不需要改变系统的其他部分。例如,Web 用户界面可以被控制台用户界面所取代,而无需改变业务规则。
  • 独立于数据库。你可以把 Oracle 或 SQL Server 换成 Mongo、BigTable、CouchDB 或其他的数据库。你的业务规则不受数据库的约束。
  • 独立于任何外部机构。事实上,你的业务规则根本不知道外界的任何情况。

更多内容请访问 https://8thlight.com/blog/uncle-bob/2012/08/13/the-clean-architecture.html

本项目有 4 个领域层:

  • Models Layer(模型层)
  • Repository Layer(存储库层)
  • Usecase Layer(用例层)
  • Delivery Layer(传送层)

如下图所示:

golang clean architecture

关于这个项目结构的原始解释,可以从这个 medium 帖子中读到:https://medium.com/@imantumorang/golang-clean-archithecture-efd6d7c43047。

可能已经不同了,但在应用层面的概念还是一样的,也可以在 Master 中看到从 v1 到当前版本的变更日志。

如何运行这个项目

确保你已经在你的 mysql 中运行了 article.sql。

由于该项目已经使用了 Go 模块,所以我建议将源代码放在 GOPATH 以外的任何文件夹中。

运行测试

$ make test

运行应用

以下是使用 docker-compose 运行的步骤:

#move to directory
$ cd workspace

# Clone into YOUR $GOPATH/src
$ git clone https://github.com/bxcodec/go-clean-arch.git

#move to project
$ cd go-clean-arch

# Build the docker image first
$ make docker

# Run the application
$ make run

# check if the containers are running
$ docker ps

# Execute the call
$ curl localhost:9090/articles

# Stop
$ make stop

使用的工具

在这个项目中,我使用了下面列出的一些工具。但是你也可以使用任何具有相同用途的类似库。但是,不同的库会有不同的实现类型。你可以发挥你的创造力,使用任何你真正需要的工具。


(First version translated and edited by vz on 2020.12.19)

Overview

Name With Ownerbxcodec/go-clean-arch
Primary LanguageGo
Program languageGo (Language Count: 3)
PlatformLinux, Mac, Windows
License:MIT License
Release Count3
Last Release Namev2.0.1 (Posted on )
First Release Namev1.0.0 (Posted on )
Created At2017-06-09 02:04:22
Pushed At2024-04-19 13:09:43
Last Commit At2024-03-08 06:46:50
Stargazers Count8.7k
Watchers Count134
Fork Count1.2k
Commits Count70
Has Issues Enabled
Issues Count49
Issue Open Count22
Pull Requests Count29
Pull Requests Open Count7
Pull Requests Close Count15
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private

go-clean-arch

Looking for the old code ?

If you are looking for the old code, you can checkout to the v1 branch

Last Updated: May 12th 2018

Description

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

Rule of Clean Architecture by Uncle Bob

  • Independent of Frameworks. The architecture does not depend on the existence of some library of feature laden software. This allows you to use such frameworks as tools, rather than having to cram your system into their limited constraints.
  • Testable. The business rules can be tested without the UI, Database, Web Server, or any other external element.
  • Independent of UI. The UI can change easily, without changing the rest of the system. A Web UI could be replaced with a console UI, for example, without changing the business rules.
  • Independent of Database. You can swap out Oracle or SQL Server, for Mongo, BigTable, CouchDB, or something else. Your business rules are not bound to the database.
  • Independent of any external agency. In fact your business rules simply don’t know anything at all about the outside world.

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

This project has 4 Domain layer :

  • Models Layer
  • Repository Layer
  • Usecase Layer
  • Delivery Layer

The diagram:

golang clean architecture

The explanation about this project's structure can read from this medium's post : https://medium.com/@imantumorang/golang-clean-archithecture-efd6d7c43047

How To Run This Project

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

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

Run the Testing

$ make test

Run the Applications

Here is the steps to run it with docker-compose

#move to directory
$ cd workspace

# Clone into YOUR $GOPATH/src
$ git clone https://github.com/bxcodec/go-clean-arch.git

#move to project
$ cd go-clean-arch

# Build the docker image first
$ make docker

# Run the application
$ make run

# check if the containers are running
$ docker ps

# Execute the call
$ curl localhost:9090/articles

# Stop
$ make stop

Tools Used:

In this project, I use some tools listed below. But you can use any simmilar 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.

Change log

To the top