Fun with Golang as a web framework
Published: 2023-07-09 13:55:39

I was recently looking for something lean, clean, and with great performance to build a SAAS app. I just needed a nice boilerplate with the usual stuff - auth, minimal backend template, user management, email templates, etc... 

Laravel - was my first choice, and [jetstream](https://jetstream.laravel.com/) seems like a great project with all the bells and whistles I need - so I set up a local copy and started to tinker with it.

After playing around with Jetstream for a while, it just didn't feel right. Don't get me wrong, Laravel is a great framework and I love using it for most of my backend stuff but for new projects - it's just gotten too bloated for my liking.

## The no framework approach

So back to square one - should I use Django? or something in the node world? I've used most of these tools before and they mostly come with batteries included, but there again is the problem. There's a price you pay for using a full stack framework - you inadvertently inherent all the bloat.

Then it dawned upon me: I use Golang for CLI tools, APIs, scrapers, etc... why not as a full backend framework?

## Golang frameworks

Golang does not have a framework as complete as Laravel or Django however there are some frameworks that are pretty cool though. Here are some I found interesting:

- [Echo](https://echo.labstack.com/)
- [Fibre](https://docs.gofiber.io/)
- [Revel](https://revel.github.io/)

And the list goes on. There are a ton of these lightweight frameworks and they all seem pretty cool. However, I picked [GIN](https://gin-gonic.com/) because it's just what I need - fast and minimal.

Surprisingly, Golang is minimal but has just enough libraries and tools to build the most common web apps and CLI tools.

You probably don't even need a framework like GIN, since Golang's built-in net/http package can generate a nice API server with just a few lines of code. Nonetheless, GIN doesn't stray too far away from stock standard Golang and provides some nice extra functionality that is missing in the default net/http package.

## Golang feels fun and productive

So far I've managed to build a boilerplate backend with: auth, SES email support, templating, etc... using just the following packages (in addition to GIN):

- Authentication (just using a sessions library plus middleware): 
https://github.com/alexedwards/scs/ to handle all the auth stuff. 
[2FA](https://github.com/pquerna/otp) auth was also pretty easy as well.
- Database handling:
 [GORM](https://gorm.io/docs/). I love the idea of having a strongly typed struct and GORM works pretty nicely with structs - you can even drop to raw SQL if needed.
 
Looking at my source code, I separated out logic into the standard MVC structure and it looks so much cleaner than most of the other full-stack frameworks (well to be fair, Django is pretty clean too). Yet, I've managed to build most of the functionality I really need and don't feel like I'm missing anything. Even more cool, since Golang is compiled, you can generate a self-contained binary; just one file to run the entire app, and no dependencies need to be installed on the production boxes.

Since Golang's syntax is so compact, didn't take me too long either to get a good base to work from.

I'm yet to add teams and billing support (I'll be using Paddle as my payment gateway) so that's going to be interesting, but so far, I'm having fun and thoroughly enjoying the whole Golang workflow too. 

The projects I'm building with this base: 
[Scriptables](https://plexscriptables.com/?ref=devto) - a neat self-hosted tool to deploy and build Laravel servers much easier.

Monolith - yeah, this is no fancy REACT + API project but merely a monolith using server-side templates with some JavaScript sprinkled throughout for those dynamic elements.

Monoliths still have a place in the web arena, and Golang is proving to be a solid alternative to more mainstream full-stack frameworks - not only is Golang super fast but also very productive. I hope more people will experiment with Golang as a full backend solution and not just as an API or micro-service layer.