Understanding the Conventional Hexagonal Architectural Structure for Go Apps

The hexagonal (or “ports and adapters”) architecture is a popular software architecture pattern you’re able to apply as you build your Go apps. Using it, you can improve your app’s scalability, maintainability, and testability.

This architecture helps separate business logic from external dependencies and infrastructure, making your application flexible and easier to maintain.

4

What Is the Hexagonal Architecture?

The hexagonal architecture divides applications into three main layers:

Advantages of Using the Hexagonal Architecture

The hexagonal architecture is popular for the following advantages it offers.

Project Scalability and Maintainability

The division of your applications allows for a modular and decoupled codebase that makes it easier to scale and maintain your app.

You can remove adapters without affecting core logic and modify the core logic without affecting the adapters. This means you can easily replace adapters without rewriting your entire application.

Dell monitor showing Windows 10 desktop

Testability and Ease of Integration

The hexagonal architecture promotes testability since you can write unit tests for core logic without mock external dependencies. You can use test doubles, such as fakes or stubs, without having toset up a databaseor message broker.

The hexagonal architecture also makes integrating your application with other systems easy. Since the adapters are separate from core logic, you can reuse them in other applications or for microservices. You can also expose your application’s port’s APIs for consumption in other systems.

firefox logo with yellow warning symbol

Flexibility and Adaptability to Changing Requirements

The hexagonal architecture provides flexibility and adaptability to changing requirements. Since the core logic is independent of the adapters, you can easily modify or extend the application’s functionality without affecting the adapters.

you could evolve your app over time, sticking to specific external systems.

MacBook and a Dell laptop running ZorinOS next to each other

Go and the Hexagonal Architecture

At the core, the hexagonal architecture is about separating the application’s core business logic from infrastructure so that you can swap out dependencies without affecting the application’s core logic, making it easier to maintain and test the application.

The typical hexagonal Go application uses four main directories:cmd,internal,pkg, andvendor.

The Golang mascot, a blue gopher, climbing a ladder superimposed on a photograph of a laptop.

Thecmddirectory contains the main applications for the project. The code you write here will typically call functions from files in the pkg and internal directories.

Theinternaldirectory should contain a private application code you don’t want users to import into their application. The Go compiler enforces the internal layout pattern, and it’s possible to have as many internal directories in other directories as you please. You’re not limited to a top-level internal directory.

Thepkgdirectory should contain the library code you want external applications to import and use. Although using thepkgdirectory is common practice, it’s not universally accepted or enforced.

Thevendordirectory should contain application dependencies (manually or automatically managed). You can use thego mod vendorcommand to create a/vendordirectory to leverage the functionalities Go provides for vendors.

Implementing the Hexagonal Architecture in Go

Your project’s file structure is important when implementing hexagonal architecture in any language, including Go.

Here’s an example file structure for implementing the Hexagonal Architecture in Go:

Thepkgdirectory contains your application’s input and output ports in this example. You’ll define interfaces for input and output ports in these files.

Theinternaldirectory contains the application domain and use cases. You’ll write your application’s business logic in these files.

Theadaptersdirectory contains the infrastructure code that connects your application to the database and API.

Setting Up the Hexagonal Architecture File Structure

Setting up your project’s hexagonal architecture file structure can be cumbersome, but you can write a bash script to automate the directory creation process.

Run this command in your project’s working directory tocreate a bash script,hexagonal.sh, and grant read, write, and execute permissions to it:

Enter this bash code inhexagonal.shto create the file structure in your current working directory:

You can run this bash script with the following command:

The bash program creates the folders and subfolders so you can proceed to create files and write the business logic for your application.

The Hexagonal Architecture Is Handy for Building Complex Applications

Implementing the hexagonal architecture can be time-consuming, but the benefits outweigh the cost in the long run. By separating concerns and making your code more modular, you can easily maintain and test your applications.

There are many other architectural patterns, each with pros and cons for building flexible, performant applications. These include the popular MVC (model, view, controller) architecture for creating web applications.

Apply Model-View-Controller design principles to your own programs for immediate results. Here’s how to get started.

These are the best free movies I found on Tubi, but there are heaps more for you to search through.

Windows is great, but adding this makes it unstoppable.

Quality apps that don’t cost anything.

Who asked for these upgrades?

These plugins will make you wonder why you used Photoshop in the first place.

Technology Explained

PC & Mobile