pangminfu / go-clean-arch

Example of how to write and clean and maintainable code in Golang using Repository pattern

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

codecov

Go Clean Architecture

Example of how to write and clean and maintainable code in Go.

Note : Currently I'm on-going updating this example to include CI/CD pipeline, Docker, mysql and other technology to share what are the best practises that I had learn so far.

Tech Involved

  1. Repository Pattern
  2. Dependency Injection
  3. Unit Test with mock (using testify/mock and mockery)
  4. Go standard project layout (refer here)
  5. Github Actions
  6. Codecov

Overview (will be update to reflect new changes)

Image of Diagram

Service : This is where all our buisness logic and validation logic located. We can write unit test to cover this to make sure any changes to this will no break anything. Service shouldn't contain any SDK(eg. AWS, GCP or others) so that when we are migrate to other cloud service or database we don't need to make any changes to Service.

Repository : This is where we will be fetch and storing our data. we can swop the implementation of repository with any others implementation(eg. Mysql, DynamoDb or others). Repository will contain SDK(eg. Mysql, DynamoDB or others) when migrate to other DB when will only need to write a new implementation of it and no changes needed on Service.

Reusable package : This will only contain Service and Repository. It will export function for Main package to call. It doesn't care about what is the implementation of the Main package.

Main package : We can make or swop our Main package implementation to any others service provider(eg. AWS Lambda or Command Line Interface) as long as we remain how we called our Reusable package Service and pass in the required dependency.

Unit Test : This will cover the Service part we should write all test case to cover all the function we had in Service. We can mock our Repository with InMemRepository and put in different dataset for testing.

Integration Test : This will need to create a Dev DB(eg. Mysql or DynamoDB) for testing.

About

Example of how to write and clean and maintainable code in Golang using Repository pattern


Languages

Language:Go 84.4%Language:TSQL 6.2%Language:Dockerfile 5.8%Language:Makefile 1.8%Language:Shell 1.8%