My project template for 2023 using Go (Fiber and MongoDB) 👨🏽‍💻

November 10th, 2022 ¿Ves algún error? Corregir artículo golang wallpaper

We're getting close to the end of 2022 and this means it's one more year in which I worked as a full-time Golang developer. During this year of work I discovered new tools that help with development flow and documentation, as well as a new way to organize my projects so they scale in a healthy way.

The Stack 🧱

We'll take a mini tour of the tools that bring this template to life and learn why I currently use them.

  • The router 🚀: For the router I chose to use Fiber due to the multiple tools it has internally developed in the framework and because I think the route declaration is much more intuitive.

  • The database 💾: For data storage I chose MongoDB for its easy deployment and mostly because the majority of projects I developed in 2022 and 2021 required me to use this database.

  • The documentation 📄: One of the most important parts when you develop a project in a professional environment is documentation - remember that you'll work with other developers. For this we'll use Swag, a Go package that allows us to generate documentation based on comments.

  • The logs ⚡️: For logs it's a combination between a tool developed in the project that allows me to have logs in a CSV file and for Logs on the server I use Zap

  • The tests 💀: For tests I used Go's native library in combination with the mocks library gomock to automatically generate mocks from my interfaces.

  • The containers 🐳: For deployment in containers I'm still using Docker for now, I'm exploring new options, if you have any recommendations leave them in the comments.

  • The deployment 🐙: For CI I'm totally in love with Github actions and its potential, I opted for Github Actions because of the ease of integration with your repository.

The file structure 📂

We have a combination between the famous Go Standard Layout repository which is an unofficial "Standard" and the package structure comes from the Hexagonal Architecture folder structure.

Within the packages we can see inside the internal/user folder that we have 3 folders application, domain and infrastructure. To keep functionalities as easy to understand as possible I have separate files for each of the functionalities.

I left this project as a template on Github in case you want to see it in more detail and as an example I implemented the entire login and authentication system using JWT.

View project.

.github

workflows

CI.yaml

cmd

http

main.go

config

env.json

deploy

dockerfile

docs

docs.go

swagger.json

swagger.yaml

hooks

pre-commit.sh

internal

user

application

application.go

create_user.go

create_user_test.go

domain

models

user.go

ports

user_application.go

user_handlers.go

user_repository.go

infrastructure

handlers

fiber.go

create_user.go

create_user_test.go

repositories

mongo.go

create_user.go

create_user_test.go

logs

log-2023-01-01.csv

pkg

middleware

application

middleware.go

authenticate.go

domain

request.go

ports.go

infrastructure

scripts

build.sh

generate-mocks.sh

.gitignore

go.mod

go.sum

main.go

README.md

Conclusions 🤔

Each year you improve as a developer exponentially if you're constantly starting new projects, it helps you think: What would I have done differently when starting this project? What tool would I have used instead of this other one? and applying them you iterate little by little until you find your perfect "template" to start a project. For now this is mine and I hope it helps you create yours according to your needs.