Building sample clean architecture golang

NamKout
Culi Tech Viet
Published in
3 min readMay 12, 2020

Hi guys, I’m a Front-End developer. Before there was a time 2 years working CMS Magento, I like CMS Magento because system clean, and architecture database EAV (Entity — Attribute — Value) but I don’t understand why present I’m dev front-end (I don’t know) in the 2-year current. And present I chance comeback working position Back-End that I like. So, I introduction project small clean architecture building golang and I’ve read some articles following:

  1. https://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-architecture.html (Uncle bob)
  2. https://medium.com/hackernoon/golang-clean-archithecture-efd6d7c43047 (Iman Tumorang — Software Engineer)
  3. https://medium.com/@eminetto/clean-architecture-using-golang-b63587aa5e3f (Elton Minetto — CTO)
Struct Project
View Struct Project

Summary

If you have read through the article Uncle bob, he said that:
> Each has at least one layer for business rules, and another for interfaces.

So, each of these architectures produce systems that are:

  1. 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.
  2. Testable. The business rules can be tested without the UI, Database, Web Server, or any other external element.
  3. 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.
  4. 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.
  5. 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

In architecture of Uncle Bob divide 4 layers:

  • Entites
  • Use Cases
  • Controller
  • Framerwork & Devices

In my project divide layers:

  • Models
  • Use case
  • Repository
  • Delivery

Models:

  • Models is same as entites. It set of data struct, interface and method, functions. Example model: Book
ModelBook

Usecase:

  • This a layer will handle problem logic business. This a layer decide repository will use. And have responsibility to provide data to layer delivery use.
  • Layer Usecase a depends layer Repository
BookUsecase

Repository:

  • This a layer will handle to database method: fetch, put, delete, get, etc.
  • Layer responsibility will choose DB use in application. Could be Mysql, Postgresql, MongoDB, MariaDB, etc
BookRepository

Deliver:

  • This a layer will decide how the data present. Could be REST API, HTML, or gRPC whatever the decide type.
  • This a lay accept the input from user and send layer Usecase handle logic
BookDelivery

Current my project building on based 4 layers: Model, Usecase, Repository, and Delivery which I present above. And my project interview, I think it not good. I will rebuild the architecture project in article next.

You should understand architecture someone and accept it in a project of you. That will give you look good and remember to start with it simple

Link Project: https://github.com/caohoangnam/go-clean-architecture
Tech stack using:
- Gorm
- Gin Framework
- Postgresql
- Viper
- Clean Architecture

If you have a question , or need more explanation, or something, that I can not explain well here, you can ask me from my linked or email me. Thank you so much.

--

--