GETH + go
Published: 2024-01-09 04:52:52

## gorm, echo, templ, htmx and go

a match made in heaven? yes.  a savior from the [endless](https://nextjs.org/) [churn](https://nuxt.com/) [of](https://nestjs.com/) [javascript](https://astro.build/) [frameworks](https://remix.run/)? yes. the opinion of a backend dev that is purely biased? yes.

### what is it

#### templ

over the past few weeks, i've been fanboying over [templ](https://github.com/a-h/templ) to the point that I'm going through projects I haven't touched in a few months and refactoring how templates work _just_ because the dev experience is so great that when I pick up the projects again for dev, I want to be set with this shiny new library.

It's no secret that there is much to be desired with templating in Go.

Using reusable components, layouts, and the like have never felt _right_ which is probably a large contributor to why you see Go suggested so much as being an excellent candidate for an API server, but not so much for a full stack server.

Enter, [templ](https://github.com/a-h/templ).  At first look, it looks reminiscent of as if JSX had a baby with Go, and as you're using it you begin to appreciate the balance of a templating language with Go mixed in between that begins to feel a bit more natural than standard library Go templates do.

#### gorm

[gorm](https://gorm.io/) has been my go-to for all things relational database in Go for as long as I can remember, and while there were some growing pains between major versions, the latest version has been _very_ stable and overall great to use.  It allows you to switch drivers with little to no effort in modifying your types, which is great where you may want to run sqlite on dev and postgres on staging/production.

#### echo

[echo](https://echo.labstack.com/) again has been a rock in my development stack for as long as I can remember.  I've tried a variety of different routers out there for Go, but keep coming back to echo.  The key wins for me here are:

- Great support for middleware, and easy API to make things extensible
- Easy binding between form, query string, and header data to make reading request bodies a breeze.
- Speed - the router has never been a bottleneck when running at scale (100's of iops or greater on a single server) - regardless of charts claiming that other routers may be faster, there's something to be said about a router that's been under active development for a long time, and is sponsored by a business - which is reassuring.

#### htmx

[htmx](https://htmx.org/) is new to my toolkit, it's something I've played around with in the past, but now with templ I find myself using it more - the templ devs are huge fans of it, and with the way that components work, using htmx's features to dynamically swap components on the page feels natural.  Something I recently learned about was [hx-boost[(https://htmx.org/attributes/hx-boost/) which is _uh-may-zing_.  It basically turns any standard SSR web application into a SPA (in theory).  You just wrap your links around an element with the attribute, and htmx handles everything else for you.  When I tested this out for the first time and saw that **it just works<sup>tm</sup>** , I was excited to say the last.

### when to use it?

In the last few weeks, this has really turned into my go-to flow.  It's easy enough to write a small handler to render templ components with echo with a small handler, and then you can either write a service to easily access your gorm DB instance within your router, or create a repository layer for more extensibility.

If you're just getting started with writing web servers and web applications in Go, I would start there - then as you're building out your application you may find places to sprinkle in htmx support, or you can jump in from the get-go with hx-boost support and make sure it's wired up from the beginning.

## what's next?

if you're interested in learning more about this stack, leave me a comment showing that you're interested - if there is enough interest then I'll put together a todo app that covers how to utilize these libraries in the simplest way possible.

as far as what other libraries I may add to this toolkit, I feel like [alpine.js](https://alpinejs.dev/) may fit naturally into this flow as well...