Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Clean Architecture using Go

Clean Architecture using Go

Presentation about Clean Architecture in Golang
Based on the post https://medium.com/@eminetto/clean-architecture-using-golang-b63587aa5e3f


Elton Minetto

March 03, 2020

More Decks by Elton Minetto

Other Decks in Programming


  1. Clean Architecture using Go Elton Minetto @eminetto

  2. 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/

  3. Constraints • Independent of Frameworks • Testable • Independent of

    UI • Independent of Database • Independent of any external agent
  4. Divide our code in 4 layers

  5. • 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.
  6. • 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
  7. Clean architecture using Go

  8. None
  9. The package entity contains our entities

  10. None
  11. In the package user we can find the other layers

    of our architecture
  12. None
  13. In the file interface.go we have the interfaces of Use

    Case and a repository, that will store our entities.
  14. None
  15. In the files repository_inmem.go and repository_mongodb.go we have two implementations

    of repository interface. The repository represents the layer Framework & Driver
  16. None
  17. In service.go we have the implementation of Use Case

  18. None
  19. The package api implements the Controller layer

  20. None
  21. In the following snippet, from api/main.go, we can see how

    to use the service
  22. None
  23. Now we can add other controllers, like a CLI app,

    lambda functions, bots, and so on.
  24. And we can test our packages, layer by layer

  25. pkg/user/service_test.go

  26. None
  27. api/handler/user_test.go

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

  30. https://eltonminetto.dev @eminetto