Slide 1

Slide 1 text

Clean Architecture using Go Elton Minetto @eminetto

Slide 2

Slide 2 text

What is Clean Architecture? • https://8thlight.com/blog/uncle-bob/ 2012/08/13/the-clean-architecture.html • https://www.amazon.com/Clean-Architecture- Craftsmans-Software-Structure/dp/ 0134494164

Slide 3

Slide 3 text

Constraints • Independent of Frameworks • Testable • Independent of UI • Independent of Database • Independent of any external agent

Slide 4

Slide 4 text

Divide our code in 4 layers

Slide 5

Slide 5 text

• Entities: encapsulate enterprise wide business rules. An entity in Go is a set of data structures and functions. • Use Cases: the software in this layer contains application specific business rules. It encapsulates and implements all of the use cases of the system.

Slide 6

Slide 6 text

• Controller: a set of adapters that convert data from the format most convenient for the use cases and entities, to the format most convenient for some external agency such as the Database or the Web • Framework & Driver: this layer is generally composed of frameworks and tools such as the Database, the Web Framework, etc

Slide 7

Slide 7 text

Clean architecture using Go

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

The package entity contains our entities

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

In the package user we can find the other layers of our architecture

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

In the file interface.go we have the interfaces of Use Case and a repository, that will store our entities.

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

In the files repository_inmem.go and repository_mongodb.go we have two implementations of repository interface. The repository represents the layer Framework & Driver

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

In service.go we have the implementation of Use Case

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

The package api implements the Controller layer

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

In the following snippet, from api/main.go, we can see how to use the service

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

Now we can add other controllers, like a CLI app, lambda functions, bots, and so on.

Slide 24

Slide 24 text

And we can test our packages, layer by layer

Slide 25

Slide 25 text

pkg/user/service_test.go

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

api/handler/user_test.go

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

Example https://github.com/eminetto/clean- architecture-go

Slide 30

Slide 30 text

https://eltonminetto.dev @eminetto