Hexagonal Architecture using Go (Fiber)👨🏽‍💻

Carlos GarcĂ­a Rosales
4 min readNov 15, 2021

--

Versión en Español

If you really wanna improve in the development time and specially in the integration of new technology to your project definitely the Hexagonal Architecture is the best decision. I implement this architecture in some languages like Java and Typescript and it’s very intuitive not happen the same with Go because Go don’t have all the “OOP” tools in the language nevertheless can be implemented with some variations in this language.

For this blog we will create a simple backend app using the most essential of the language. first we need to know the folder structure.

Hexagonal Architecture Folder Structure

In Go we don’t have Class but we have Structs and this have special functions can be assigned to this we will use this as a Method of a Class check the differences in a Class and Struct with Methods.

Differences of a Class and Struct with Typescript and Go

As you see in the image Go Struct don’t have explicit constructor as Typescript have so we simulated a Constructor function for create a instance of User (NewPerson in the left image) and then we create a “Method” of the structure (GetEmail in the left image) now we have the way for replace the Classes so we can start to build our app.

1. Creating the Domains: User

We will use the previous Struct used in the image but this time we will create it using Github gist for make our live more easy. The domain more easy analogy is basically your Models.

2. Creating the ports: UserRepository, UserService and UserHandlers

The ports are the instructions of how a Service or Repository should behavior , in this part you say it what methods a Struct must have for be considered a Repository or Service . This part of the Software just do this is The Book Rule, the Supervisor, etc. Don’t eat your mind. Lucky for us Go have interfaces, but work a little bit different they are implicit so we don’t say this struct implements this interface we just implement the methods in the struct and automatically this struct ca be considered as a amazing member of the interface.

3. Creating the services: UserService implemented

Now thanks to the interface we know what methods we will need for create our UserService we need this Struct have a Login method and Register method. But this user service should communicate with the Repository right! 🤔.. so How we reach this? In the UserService struct we will use we need to add this need a property of type UserRepository for work. Trust me when we will initialize the program you will understand this part.

4. Creating the Repositories: UserRepository

The user repository struct will communicate with the database so in the creation function (NewUserRepository) we should add the Database initialization for this project we will use mongo don’t think too much about this is just for the example.

5. Creating Handlers: UserHandlers

Now we need to expose our service to the handlers and this handlers to the world I will use GoFiber for this as in the other implementations we will do something similar but this time the UserHandler struct will need of the UserService, because the handlers will communicate with the services.

6. Creating the Server: Fiber HTTPS server

After we have all the Lego blocks ready wee need to create a last one for initialize our program in a HTTPS server, I will continue using Fiber for this. As in the other parts of the program I will use a Struct for make this more modular.

7. Lets the magic start!

Finally we will create our main.go file and we will understand better how this work. Remember this for create our UserRepository we need a MongoConn string, for create our UserService we need a UserRepository, for create our UserHandler we need UserService and Finally for create a Server we need the handlers in this case we only need UserHandlers.

MongoConn:string > UserRepository > UserService > UserHandler > Server

8. Enjoy the moment.

Finally we have our Go app builded in Hexagonal Architecture and with Fiber, I will upload all the code to Github for use it as a template I hope you enjoy the Blog and if you like it please give me a clap đź‘€ that motivate me for continue writing this blogs.

https://github.com/solrac97gr/goHexagonalBlog

--

--

Carlos GarcĂ­a Rosales
Carlos GarcĂ­a Rosales

Written by Carlos GarcĂ­a Rosales

I am Software Developer working in @bayonet.io using the amazing programming language Go! I hope help you with my articles. follow me in my social medias!

Responses (6)