Go Microservice with Clean Architecture

Jin Feng
4 min readJul 23, 2019

--

Update (08/04/2020)

I have made a major upgrade to this framework. Although the changes made are small, the improvement is remarkable. This update fixes all the major problems in the old framework, although its main project structure and interfaces have not changed. For specific changes, please refer to “Go Microservice with Clean Architecture-A Major Upgrade”. Now it has almost everything in my ideal framework. It is a lightweight framework, but powerful and can plug any new components into it. I wrote the following four articles to describe the new framework.

1. A Self-Evolved Microservice Framework in Go

2. A Non-Intrusive Transaction Management Lib in Go — How to Use It

3. A Non-Intrusive Transaction Management Lib in Go — How it Works?

4. Go Microservice with Clean Architecture-A Major Upgrade

If you want to start a new project, then I suggest you start with the new framework. As for the articles I wrote about the old framework, most of them are still valid. There are three models in the new framework, from simple to complex. The old framework is based on the third model, which is the most complex. Since the new article only covers the new content, the old article still needs to be read. As for the reading order, it doesn’t matter whether you read the new one first or the old one first. If you haven’t read anyone, then you can start with the new one and then read the old one. If you have read the old one before the new one, then the whole context may be clearer. If there are some inconsistencies in some articles, the new article shall prevail. There are more changes in transaction management and log management, and there are also some minor changes to the project structure.

Original Article (07/23/2019)

I created a Microservice project with Go and gRPC and tried to figure out what is the best project layout, which can be used as a template for my future projects. I also tried to apply the best practice in application design and programming, such as Clean Architecture, Dependency Injection, logging, error handling, and so on, to the Go Microservice application. I came from a Java background and found myself keeping struggling between Java and Go idioms, which are quite different. I wrote a series of articles about decisions and trade-offs I made when working on the project.

Who is the audience?

Knowing basic Go is not required for reading the articles, but definitely will help you. If you don’t know Go and can’t figure what the code is doing, then you probably want to learn some basic Go from “Go by Example”¹ (You don’t need to finish all of the topics, just the first several). The “Transaction support” one in this series is database heavy and requires some database knowledge in Go and you can acquire it from “Go database/SQL tutorial”². If you are interested in Go Microservice programming and thinking about the code design and application layers, project structure, coding style, logging, transaction management, and Dependency Injection, then this series is perfect for you.

What is it about?

The series is not about how to create a quick working prototype, but about how to create an application with a good design that easily adapts to changes, for example, you may want to split part of functions into a separate Microservice, or add transaction support after a business feature is up-running, or switch to a better logger, but don’t want to change every logging statement in your code. By applying the design in this project, the business logic code is never touched when making the above changes. You seldom change any existing code (except for the container code), but only add new code, thus the QA effort was greatly reduced. You can use this application as the starting point to base your next application. The only cost is to figure out how different pieces of the application are wired together. If you have a Spring (Java) background or coming from the object-oriented paradigm, or know Clean Architecture or SOLID, the code should be pretty familiar to you.

You don’t need to read articles in the following order. If you are familiar with Clean Architecture³ or SOLID⁴, you can read the articles in any order without a problem. However, I do recommend you start from the first one, which will give you an overview of the whole project, then you can pick the ones you are interested in.

  1. Go Microservice with Clean Architecture: Application Layout
  2. Go Microservice with Clean Architecture: Application Design
  3. Go Microservice with Clean Architecture: Design Principle
  4. Go Microservice with Clean Architecture: Coding Style
  5. Go Microservice with Clean Architecture: Transaction Support
  6. Go Microservice with Clean Architecture: Application Logging
  7. Go Microservice with Clean Architecture: Application Container
  8. Go Microservice with Clean architecture: Dependency Injection
  9. How to Do Tracing in Go Microservice?
  10. Service Resilience in Go Microservice

Source Code:

The complete code is in GitHub: https://github.com/jfeng45/servicetmpl

Reference:

[1] Go by Example
https://gobyexample.com/

[2] Go database/sql tutorial
http://go-database-sql.org/index.html

[3]The Clean Code Blog
https://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-architecture.html

[4] S.O.L.I.D is for the first five object-oriented design (OOD) principles introduced by Robert C. Martin, popularly known as Uncle Bob and the acronym is introduced later by Michael Feathers.
Bob.http://butunclebob.com/ArticleS.UncleBob.PrinciplesOfOod

--

--

Jin Feng

Writing applications in Java and Go. Recent interests including application design and Microservice.