Clean Architecture With GO. Introducing Clean Architecture With Go. - by Manato Kuroda - Medium
Clean Architecture With GO. Introducing Clean Architecture With Go. - by Manato Kuroda - Medium
Manato Kuroda
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
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:
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:
So if you are not familiar with it, you can read some
recommended articles to catch up.
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
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
easily testable
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
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
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 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.
Use Cases
Use cases contain application business rules using a
domain model and have Input Port and Output Port.
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.
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
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
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
interface.
And a usecase/presenter/user_presenter.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
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
And router/router.go :
Registry
registry plays a role in resolving dependencies using
constructor injection.
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
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
manakuro/golang-clean-architecture
Go with Clean Architecture. Contribute
to manakuro/golang-clean-…
github.com
https://manakuro.medium.com/clean-architecture-with-go-bce409427d31 16/16