Trying Clean Architecture on Golang by@imantumorang
99,008 reads

Trying Clean Architecture on Golang

June 6th 2017
7 min
by @imantumorang 99,008 reads
tldt arrow
Read on Terminal Reader

Too Long; Didn't Read

After reading uncle Bob’s Clean Architecture Concept, I’m trying to implement it in Golang. This is a similar architecture that we used in our company, Kurio - App Berita Indonesia, but a little different structure. The architecture does not depend on the existence of some library of feature laden software. The business rules can be tested without the UI, Database, Web Server, or any other external element. You could replace anything here, with your own or third party that has the same functions.

Companies Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - Trying Clean Architecture on Golang
Iman Tumorang HackerNoon profile picture


Iman Tumorang

Software Engineer

Learn More
react to story with heart

Independent, Testable , and Clean

After reading the uncle Bob’s Clean Architecture Concept, I’m trying to implement it in Golang. This is a similar architecture that we used in our company, Kurio - App Berita Indonesia, but a little different structure. Not too different, same concept but different in folder structure.

You can look for a sample project here, a sample CRUD management article.

Disclaimer : 
I’m not recommending any library or framework used here. You could replace anything here, with your own or third party that has the same functions.


As we know the constraint before designing the Clean Architecture are :

  1. Independent of Frameworks. The architecture does not depend on the existence of some library of feature laden software. This allows you to use such frameworks as tools, rather than having to cram your system into their limited constraints.
  2. Testable. The business rules can be tested without the UI, Database, Web Server, or any other external element.
  3. Independent of UI. The UI can change easily, without changing the rest of the system. A Web UI could be replaced with a console UI, for example, without changing the business rules.
  4. Independent of Database. You can swap out Oracle or SQL Server, for Mongo, BigTable, CouchDB, or something else. Your business rules are not bound to the database.
  5. Independent of any external agency. In fact your business rules simply don’t know anything at all about the outside world.More at

So, based on this constraint, every layer must independent and testable.

If Uncle Bob’s Architecture, has 4 layer :

  • Entities
  • Usecase
  • Controller
  • Framework & Driver

In my projects, I’m using 4 too :

  • Models
  • Repository
  • Usecase
  • Delivery


Same as Entities, will used in all layer. This layer, will store any Object’s Struct and its method. Example : Article, Student, Book.
Example struct :

import "time"

type Article struct {
	ID        int64     `json:"id"`
	Title     string    `json:"title"`
	Content   string    `json:"content"`
	UpdatedAt time.Time `json:"updated_at"`
	CreatedAt time.Time `json:"created_at"`

Any entities, or model will stored here.


Repository will store any Database handler. Querying, or Creating/ Inserting into any database will stored here. This layer will act for CRUD to database only. No business process happen here. Only plain function to Database.

This layer also have responsibility to choose what DB will used in Application. Could be Mysql, MongoDB, MariaDB, Postgresql whatever, will decided here.

If using ORM, this layer will control the input, and give it directly to ORM services.

If calling microservices, will handled here. Create HTTP Request to other services, and sanitize the data. This layer, must fully act as a repository. Handle all data input - output no specific logic happen.

This Repository layer will depends to Connected DB , or other microservices if exists.


This layer will act as the business process handler. Any process will handled here. This layer will decide, which repository layer will use. And have responsibility to provide data to serve into delivery. Process the data doing calculation or anything will done here.

Usecase layer will accept any input from Delivery layer, that already sanitized, then process the input could be storing into DB , or Fetching from DB ,etc.

This Usecase layer will depends to Repository Layer


This layer will act as the presenter. Decide how the data will presented. Could be as REST API, or HTML File, or gRPC whatever the delivery type. 
This layer also will accept the input from user. Sanitize the input and sent it to Usecase layer.

For my sample project, I’m using REST API as the delivery method. 
Client will call the resource endpoint over network, and the Delivery layer will get the input or request, and sent it to Usecase Layer.

This layer will depends to Usecase Layer.

Communications Between Layer

Except Models, each layer will communicate through inteface. For example, Usecase layer need the Repository layer, so how they communicate? Repository will provide an interface to be their contract and communication.

Example of Repository’s Interface

package repository

import models ""

type ArticleRepository interface {
	Fetch(cursor string, num int64) ([]*models.Article, error)
	GetByID(id int64) (*models.Article, error)
	GetByTitle(title string) (*models.Article, error)
	Update(article *models.Article) (*models.Article, error)
	Store(a *models.Article) (int64, error)
	Delete(id int64) (bool, error)

Usecase layer will communicate to Repository using this contract, and Repository layer MUST implement this interface so can used by Usecase

Example of Usecase’s Interface

package usecase

import (

type ArticleUsecase interface {
	Fetch(cursor string, num int64) ([]*article.Article, string, error)
	GetByID(id int64) (*article.Article, error)
	Update(ar *article.Article) (*article.Article, error)
	GetByTitle(title string) (*article.Article, error)
	Store(*article.Article) (*article.Article, error)
	Delete(id int64) (bool, error)

Same with Usecase, Delivery layer will use this contract interface. And Usecase layer MUST implement this interface.

Testing Each Layer

As we know, clean means independent. Each layer testable even other layers doesn’t exist yet.

  • Models Layer: This layer only tested if any function/method declared in any of Struct. And can test easily and independent to other layers.
  • Repository: To test this layer, the better ways is doing Integrations testing. But you also can doing mocking for each test. I’m using as my helper to mock query process msyql.
  • Usecase: Because this layer depends to Repository layer, means this layer need Repository layer for testing . So we must make a mockup of Repository that mocked with mockery, based on the contract interface defined before.
  • Delivery: Same with Usecase, because this layer depends to Usecase layer, means we need Usecase layer for testing. And Usecase layer also must mocked with mockery, based on the contract interface defined before

For mocking , I use mockery for golang by vektra could be seen here

Repository Test

To test this layer, like I said before , I’m using a sql-mock to mock my query process. You can use like what I used here , or another that have similar function

func TestGetByID(t *testing.T) {
 db, mock, err := sqlmock.New() 
 if err != nil { 
    t.Fatalf(“an error ‘%s’ was not expected when opening a stub  
        database connection”, err) 
 defer db.Close() 
 rows := sqlmock.NewRows([]string{
        “id”, “title”, “content”, “updated_at”, “created_at”}).   
        AddRow(1, “title 1”, “Content 1”, time.Now(), time.Now()) 
 query := “SELECT id,title,content,updated_at, created_at FROM 
          article WHERE ID = \\?” 
 a := articleRepo.NewMysqlArticleRepository(db) 
 num := int64(1) 
 anArticle, err := a.GetByID(num) 
 assert.NoError(t, err) 
 assert.NotNil(t, anArticle)

Usecase Test

Sample test for Usecase layer, that depends to Repository layer.

package usecase_test

import (

	models ""
	ucase ""

func TestFetch(t *testing.T) {
	mockArticleRepo := new(mocks.ArticleRepository)
	var mockArticle models.Article
	err := faker.FakeData(&mockArticle)
	assert.NoError(t, err)

	mockListArtilce := make([]*models.Article, 0)
	mockListArtilce = append(mockListArtilce, &mockArticle)
	mockArticleRepo.On("Fetch", mock.AnythingOfType("string"), mock.AnythingOfType("int64")).Return(mockListArtilce, nil)
	u := ucase.NewArticleUsecase(mockArticleRepo)
	num := int64(1)
	cursor := "12"
	list, nextCursor, err := u.Fetch(cursor, num)
	cursorExpected := strconv.Itoa(int(mockArticle.ID))
	assert.Equal(t, cursorExpected, nextCursor)
	assert.NotEmpty(t, nextCursor)
	assert.NoError(t, err)
	assert.Len(t, list, len(mockListArtilce))

	mockArticleRepo.AssertCalled(t, "Fetch", mock.AnythingOfType("string"), mock.AnythingOfType("int64"))


Mockery will generate a mockup of repository layer for me. So I don’t need to finish my Repository layer first. I can work finishing my Usecase first even my Repository layer not implemented yet.

Delivery Test

Delivery test will depends on how you to deliver the data. If using http REST API, we can use httptest a builtin package for httptest in golang.

Because it’s depends to Usecase, so we need a mock of Usecase . Same with Repository, i’m also using Mockery to mock my usecase, for delivery testing.

func TestGetByID(t *testing.T) {
 var mockArticle models.Article 
 err := faker.FakeData(&mockArticle) 
 assert.NoError(t, err) 
 mockUCase := new(mocks.ArticleUsecase) 
 num := int(mockArticle.ID) 
 mockUCase.On(“GetByID”, int64(num)).Return(&mockArticle, nil) 
 e := echo.New() 
 req, err := http.NewRequest(echo.GET, “/article/” +  
             strconv.Itoa(int(num)), strings.NewReader(“”)) 
 assert.NoError(t, err) 
 rec := httptest.NewRecorder() 
 c := e.NewContext(req, rec) 
 handler:= articleHttp.ArticleHandler{
            AUsecase: mockUCase,
            Helper: httpHelper.HttpHelper{}
 assert.Equal(t, http.StatusOK, rec.Code) 
 mockUCase.AssertCalled(t, “GetByID”, int64(num))

Final Output and The Merging

After finish all layer and already passed on testing. You should merge into one system in main.go in root project. 
Here you will define, and create every needs to environment, and merge all layers into one.

Look for my main.go as example:

package main

import (

	httpDeliver ""
	articleRepo ""
	articleUcase ""
	cfg ""
	_ ""

var config cfg.Config

func init() {
	config = cfg.NewViperConfig()

	if config.GetBool(`debug`) {
		fmt.Println("Service RUN on DEBUG mode")


func main() {

	dbHost := config.GetString(``)
	dbPort := config.GetString(`database.port`)
	dbUser := config.GetString(`database.user`)
	dbPass := config.GetString(`database.pass`)
	dbName := config.GetString(``)
	connection := fmt.Sprintf("%s:%[email protected](%s:%s)/%s", dbUser, dbPass, dbHost, dbPort, dbName)
	val := url.Values{}
	val.Add("parseTime", "1")
	val.Add("loc", "Asia/Jakarta")
	dsn := fmt.Sprintf("%s?%s", connection, val.Encode())
	dbConn, err := sql.Open(`mysql`, dsn)
	if err != nil && config.GetBool("debug") {
	defer dbConn.Close()
	e := echo.New()
	middL := middleware.InitMiddleware()

	ar := articleRepo.NewMysqlArticleRepository(dbConn)
	au := articleUcase.NewArticleUsecase(ar)

	httpDeliver.NewArticleHttpHandler(e, au)


You can see, every layer merge into one with its dependencies.


  • In short, if drawn in a diagram, can seen below
  • image

  • Every library used here you can change by your own. Because the main point of clean architecture is : no matter your library, but your architecture is clean, and testable also independent
  • This is how i organized my projects, you could argue, or agree, or maybe improve this for more better, just leave a comment and share this

The Sample Projects

The sample project can seen here

Library used for my project:

  • Glide : for package management
  • go-sqlmock from
  • Testify : for testing
  • Echo Labstack (Golang Web Framework) for Delivery layer
  • Viper : for environment configurations

Further Reading about Clean Architecture:

  • Second part of this article:
  • . Another version of Clean Architecture in Golang

If you have a question , or need more explanation, or something, that I can not explain well here, you can ask me from my linkedin or email me. Thank you


. . . comments & more!
Hackernoon hq - po box 2206, edwards, colorado 81632, usa