Hexagonal Architecture in Go

Jemesson Lima
Hexagonal Architecture in Go
3 min readNov 3, 2021

This year, I have been learning and practicing more about Software Architecture. There are many posts on this subject, but I mainly recommend the Software Architecture Chronicles from Herberto Graça.

One of the topics covered in these chronicles is: Ports & Adapters (aka Hexagonal Architecture).

Summary

The Ports & Adapters Architecture was thought of by Alistair Cockburn and written down on his blog in 2005. This is how he defines its goal in one sentence:

Allows an application to equally be driven by users, programs, automated test or batch scripts, and to be developed and tested in isolation from its eventual run-time devices and databases.

The main idea in this architecture is to separate the concerns, each component has a well-defined purpose. Focus on the Core business and the external services will plug and play with the application through ports and adapters.

The hexagonal architecture is agnostic of the framework and programming language.

Ports

A port is a consumer agnostic entry and exit point to/from the Core business. In many languages, it will be an interface. For example, it can be an interface to perform searches in a search engine.

Adapters

Adapters can be external services that wants to communicate with the application business logic.

Example 1: An input adapter could be a web interface. When a user clicks a button, the web adapter calls a certain input port to communicate with the core.

Example 2: An output adapter adapters are called by our core business, for instance, persisting data into a database.

Hexagonal architecture

Pros:

  1. Separation of concerns
  2. Easily to test the application
  3. Easily to exchange technologies
  4. Easily to exchange infrastructure

Cons:

  1. Can be complex for small or short-term projects.
  2. There is no guidance on organizing code. There are conventions.

I strongly recommend to start a new app using an architecture. The Hexagonal architecture can help your team for parallelization of work, testing in isolation and to focus in the Core business.

Hands On

Last month I decided to learn a new language, Go. It is an open source programming language that makes it easy to build simple, reliable, and efficient software. Then I took the opportunity to practice my knowledges about the Hexagonal Architecture.

Inspired in the article from Matías Varela, I rewrote the MinesWeeper API using my own approach with refactorings, and to make each component easier to understand.

Note: Checkout my repository on Github to fully understand the application. Feel free opening a Pull Request or creating a new issue.

Next chapters:

The next post will be really amazing and I’ll bring an important topic: Event Driven Architecture (EDA).

--

--