0% found this document useful (0 votes)
175 views

Clean Architecture With GO. Introducing Clean Architecture With Go. - by Manato Kuroda - Medium

The document discusses Clean Architecture and how to implement it in Go. Clean Architecture aims to separate concerns and organize code into layers with explicit rules to create testable and maintainable projects. It highlights two key rules - dependencies can only point inward, and layers should depend on abstract interfaces rather than concrete implementations. The article then outlines the different layers in Clean Architecture - entities, use cases, interface adapters, controllers, presenters and frameworks/drivers.

Uploaded by

Ahmad As Syuaiby
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
175 views

Clean Architecture With GO. Introducing Clean Architecture With Go. - by Manato Kuroda - Medium

The document discusses Clean Architecture and how to implement it in Go. Clean Architecture aims to separate concerns and organize code into layers with explicit rules to create testable and maintainable projects. It highlights two key rules - dependencies can only point inward, and layers should depend on abstract interfaces rather than concrete implementations. The article then outlines the different layers in Clean Architecture - entities, use cases, interface adapters, controllers, presenters and frameworks/drivers.

Uploaded by

Ahmad As Syuaiby
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

4/7/2021 Clean Architecture with GO. Introducing Clean architecture with Go.

| by Manato Kuroda | Medium

Get started Open in app

Manato Kuroda

Follow 279 Followers About

You have 2 free member-only stories left this month.


Sign up for Medium and get an extra one

Clean Architecture with GO


Introducing Clean architecture with Go.

Manato Kuroda Nov 7, 2019 · 7 min read

https://manakuro.medium.com/clean-architecture-with-go-bce409427d31 1/16
4/7/2021 Clean Architecture with GO. Introducing Clean architecture with Go. | by Manato Kuroda | Medium

Get started Open in app


Clean Architecture with Go

Clean Architecture is designed to separate concerns by


organizing code into several layers with a very explicit rule
which enables us to create a testable and maintainable
project. In this article, I’m going to highlight how Clean
Architecture works in Go.

Example repo
You can view the full codebase at:

manakuro/golang-clean-architecture
Go with Clean Architecture. Contribute
to manakuro/golang-clean-…
github.com

Updated:

Add transactions support

https://manakuro.medium.com/clean-architecture-with-go-bce409427d31 2/16
4/7/2021 Clean Architecture with GO. Introducing Clean architecture with Go. | by Manato Kuroda | Medium

Pre-requisites
Get started Open in app
The target of readers in this post is who:

knows the basic idea of Clean Architecture

wants to implement Go with Clean Architecture

So if you are not familiar with it, you can read some
recommended articles to catch up.

The Clean Code Blog

Clean Architecture: Part 2 — The Clean Architecture

Better Software Design with Clean Architecture

The darkness of software design


If you’ve been working as a programmer, you’ve probably
run into the code that:

is painful to add new features

is too difficult to debug

is hard or impossible to test without dependencies like


a web server or database

get a view and business logic mixed in widely, even


can’t be separated

https://manakuro.medium.com/clean-architecture-with-go-bce409427d31 3/16
4/7/2021 Clean Architecture with GO. Introducing Clean architecture with Go. | by Manato Kuroda | Medium

is hard to understand what its purpose for


Get started Open in app
has a lot of jobs only in one function and too long.

You might be even scared of committing a tiny little change


because you don’t know how much it affects other
functions and there are no unit tests, even if there is, it’s too
complicated and mysterious, which means useless. Even if
it is structured in MVC or MVVM, there are still issues
around the project, of which business logic is leaking into
controllers, the domain model is used across the entire
project for different purposes and so on.

Advantages in Clean Architecture

https://manakuro.medium.com/clean-architecture-with-go-bce409427d31 4/16
4/7/2021 Clean Architecture with GO. Introducing Clean architecture with Go. | by Manato Kuroda | Medium

The Clean Architecture


Get started Open in app

Clean Architecture is one of the software designs of


organizing the codebase and provides a solution to these
issues you’ve ever seen. By introducing Clean Architecture
you’ll get your code:

highly decoupled from any UI, web framework or the


database

focusing more on business logic

easily testable

maintainable, visible and easier to understand

In Clean Architecture the codebase should be flexible and


portable. It should not be dependent on any specific web
framework or database, which means you could switch it to
an entirely new platform. For instance, at the beginning of
the project you use RDB but somehow even if you are
forced to replace it with NoSQL for some reason, you can
switch it without changing any business logic.

The only rules you need to know


When you implement Clean Architecture, you might try to
follow the circle diagram completely, but actually, the
author said:

https://manakuro.medium.com/clean-architecture-with-go-bce409427d31 5/16
4/7/2021 Clean Architecture with GO. Introducing Clean architecture with Go. | by Manato Kuroda | Medium

Only Four Circles? No, the circles are schematic… There’s no


Get started Open in app
rule that says you must always have just these four…

So you don’t need to implement it as it is, just takes it as an


example for understanding the architecture. But there are
important rules that you need to follow.

1. The dependencies can only point inward


In Clean Architecture the details like a web framework and
databases are in the outer layers while important business
rules are in the inner circles and have no knowledge of
anything outside world. Following Acyclic Dependencies
Principle(ADP), the dependencies only point inward in the
circle, not point outward and no circulation.

https://manakuro.medium.com/clean-architecture-with-go-bce409427d31 6/16
4/7/2021 Clean Architecture with GO. Introducing Clean architecture with Go. | by Manato Kuroda | Medium

Get started The


Opendependencies
in app only point inward

2. Separation of details and abstracts


The details in Clean Architecture are the data, framework,
database, and API. Using the details in the core layer means
it violates The Dependency Rule. It should always be
dependent on an abstract interface not specific knowledge
of the details so it will be flexible and maintainable. As seen
at the right of the diagram, it shows that Controller and
Presenter are dependent on Use Case Input Port and
Output Port which is defined as an interface, not specific
logic(the details). But how is it possible to work without
knowing the details in the outer layer?

https://manakuro.medium.com/clean-architecture-with-go-bce409427d31 7/16
4/7/2021 Clean Architecture with GO. Introducing Clean architecture with Go. | by Manato Kuroda | Medium

The flow of control


Get started Open in app

The Dependency Inversion Principle (DIP) resolves this


contradiction without violating the rules. If you are not
sure about DIP, you can check out some articles before
proceeding.

The Layers
We’ve learned a basic of the rule in Clean Architecture.
Next, take a look at the layers before implementing them.

Entities
Entities is a domain model that has wide enterprise
business rules and can be a set of data structures and
functions.

ex.) A struct type of user, book and author.

Use Cases
Use cases contain application business rules using a
domain model and have Input Port and Output Port.

Input Port is in charge of handling data from the outer


layer and defined as abstract.

Output Port is in charge of handling data from Use cases to


the outer layer and defined as abstract.

https://manakuro.medium.com/clean-architecture-with-go-bce409427d31 8/16
4/7/2021 Clean Architecture with GO. Introducing Clean architecture with Go. | by Manato Kuroda | Medium

Interface Adapter
Get started Open in app
Interface Adapter handles the communication with the
inner and the outer layer. It has only concerns about
technological logic, not business logic.

Controllers are a set of a specific implementation of Input


Port in Use Cases.

ex.) Convert form data before saving it in database

Presenter is a set of a specific implementation of Output


Port in Use Cases.

ex.) Convert data from the database before passing to View

Frameworks and Drivers


Frameworks and drivers contain tools like databases,
frameworks or API and basically do not have very much
code.

ex.) API, database and web framework

Implementation of Clean Architecture in GO


Next, we’ll introduce Clean Architecture to Go and see how
this works using a real use case. Let’s suppose that we’ll
create a simple API responding to user data.

Now that we have the project structure as below:

https://manakuro.medium.com/clean-architecture-with-go-bce409427d31 9/16
4/7/2021 Clean Architecture with GO. Introducing Clean architecture with Go. | by Manato Kuroda | Medium

. started
Get Open in app
├── domain
│ └── model
│ └── user.go
├── infrastructure
│ ├── datastore
│ │ └── db.go
│ └── router
│ └── router.go
├── interface
│ ├── controller
│ │ ├── app_controller.go
│ │ ├── context.go
│ │ └── user_controller.go
│ ├── presenter
│ │ └── user_presenter.go
│ └── repository
│ └── user_repository.go
├── main.go
├── registry
│ ├── registry.go
│ └── user_registry.go
├── usecase
│ ├── presenter
│ │ └── user_presenter.go
│ ├── repository
│ │ └── user_repository.go
│ └── interactor
│ └── user_interactor.go

Each directory has a role of each layer as following:

https://manakuro.medium.com/clean-architecture-with-go-bce409427d31 10/16
4/7/2021 Clean Architecture with GO. Introducing Clean architecture with Go. | by Manato Kuroda | Medium

Get started Open in app

Directory and Layer

There’s no rule that you must always have just these four
layers in Clean Architecture so actually, it depends on the
teams or the projects.

Entities Layer
First off, we create a user domain model as Entities layer
in domain/model/user.go :

1 package model
2
3 import "time"
4
5 type User struct {
6 ID uint `gorm:"primary_key" json:"id"
7 Name string `json:"name"`
8 Age string `json:"age"`
9 CreatedAt time.Time `json:"created_at"`
10 UpdatedAt time.Time `json:"updated_at"`
11 DeletedAt time.Time `json:"deleted at"`

https://manakuro.medium.com/clean-architecture-with-go-bce409427d31 11/16
4/7/2021 Clean Architecture with GO. Introducing Clean architecture with Go. | by Manato Kuroda | Medium

Use Cases Layer


Get started Open in app
In Use cases layer we have three directories, repository ,

presenter and interactor . interactor is in charge of


Input Port and presenter is in charge of Output Port.
interactor has a set of methods of specific application
business rules depending on repository and presenter

interface.

Let’s create a findAll interface in


usecase/repository/user_repository.go :

And a usecase/presenter/user_presenter.go :

Create a Get as a function of finding all users in


usecase/interactor/user_interactor.go :

Interface Adapter Layer


In Interface Adapter layer, there are controllers ,
presenters and repository folders. controllers is in
charge of the C of MVC model and handles API requests
that come from the outer layer. repository is a specific
implementation of repository in Use Cases and stores any
database handler as Gateway.

Let’s create interface/controllers/user_controller.go :

https://manakuro.medium.com/clean-architecture-with-go-bce409427d31 12/16
4/7/2021 Clean Architecture with GO. Introducing Clean architecture with Go. | by Manato Kuroda | Medium

In GetUsers it calls interactor in usecase and respond to


Get started Open in app
user data.

And we add interface/controllers/app_controller.go :

That is a set of controllers defined to use in


infrastructure/router/router.go later.

Next, create interface/presenters/user_presenter.go :

ResponseUsers handles user data before passing it to view


basically but in this case, we add some text to the user
name before responding to the client.

And add interface/repository/user_repository.go :

FindAll is an actual implementation of the interface in


usecase/repository/user_repository.go that we’ve defined
above and it’ll be injected as a method of handling
database.

Frameworks and Drivers Layer


We have datastore and router in infrastructure.
datastore is used as creating a database instance, in this
case, we use gorm with MySQL. router is defined as a
routing request using echo.

https://manakuro.medium.com/clean-architecture-with-go-bce409427d31 13/16
4/7/2021 Clean Architecture with GO. Introducing Clean architecture with Go. | by Manato Kuroda | Medium

Let’s create datastore/db.go :


Get started Open in app

And router/router.go :

Registry
registry plays a role in resolving dependencies using
constructor injection.

Let’s create registry/registry.go :

NewRegistry takes a db instance to pass down to


interface/repository .

And add registry/user_registry.go :

NewUserController generates a controller with interactor.


NewUserInteractor returns an interactor with a repository
and presenter. NewUserRepository returns repository of
interface passed with a database instance which fulfills
usecase interface.

Booting
Now that we’re ready to boot a server with users endpoint.
Let’s add some code to main.go :

https://manakuro.medium.com/clean-architecture-with-go-bce409427d31 14/16
4/7/2021 Clean Architecture with GO. Introducing Clean architecture with Go. | by Manato Kuroda | Medium

We have echo framework for routing and pass it to router


Get started Open in app
and controllers created by registry.NewRegistry .

Let’s try to see how it works at


http://localhost:8080/users : (Make sure that there’s
some record in your database)

It seems to work fine.

Conclusion
That’s it. We’ve created a very simple API responding to a
user data with Clean Architecture. I like the idea of
separating the different aspects of the product as highly
decoupled layers.
https://manakuro.medium.com/clean-architecture-with-go-bce409427d31 15/16
4/7/2021 Clean Architecture with GO. Introducing Clean architecture with Go. | by Manato Kuroda | Medium

I hope you will find the benefits I highlighted in this article.


Get started Open in app

You can view the final codebase at:

manakuro/golang-clean-architecture
Go with Clean Architecture. Contribute
to manakuro/golang-clean-…
github.com

Go Clean Architecture API Architecture

About Help Legal

Get the Medium app

https://manakuro.medium.com/clean-architecture-with-go-bce409427d31 16/16

You might also like