valfz

course

Server-rendered apps with Go, templ and HTMX

Build an interactive web app where the server renders HTML and the browser swaps fragments, using Go's standard library, templ, htmx and SQLite.

topic
go and htmx
level
intermediate
lessons
8
read
2 h 5 min

This course is for developers who can write a small Go program and read HTML, and who want to build interactive web applications without a JavaScript framework or a JSON API in between. The server renders HTML. The browser asks for fragments of it and swaps them into the page.

Across eight lessons you build one application, a reading list: add a link with a title and a note, mark it as read, edit it in place, delete it, search and filter. It starts as a single net/http handler and ends as one self-contained binary with a SQLite database, tests and an orderly shutdown.

The code was written and tested with these versions:

  • Go 1.27.3. The last lesson uses http.CrossOriginProtection, which needs Go 1.25 or later.
  • templ v0.3.1020, for both the command-line tool and the Go module.
  • htmx 4.0.2. Version 4.0 was released in August 2026, while npm’s latest tag still points at the 2.x line. It differs from 2.x in its request headers, in attribute inheritance and in how it treats error responses. Everything here is 4.0.2 behavior, checked in a browser. The article What htmx actually does covers the same version and the differences.
  • Alpine.js 3.17.5, for one small piece of client-only state.
  • modernc.org/sqlite v1.63.0, a SQLite driver that needs no C compiler.

To follow along, type the code into a fresh directory as the lessons introduce it and run the commands shown. Each lesson starts from the state the previous one left and ends with a working application. You need curl, and a browser from lesson 3 on. Every lesson closes with exercises that come without solutions but say what a correct result looks like.

What you build

A reading list for saving, searching, editing and marking links, backed by SQLite and shipped as one binary.

Before you start

  • You can write and run a small Go program: packages, structs, methods, errors.
  • You can read HTML and know what a form submission does.
  • You have used curl or a similar tool to make an HTTP request.

After the last lesson you can

  • Route requests with http.ServeMux patterns and structure handlers and middleware without a framework.
  • Write templ components and render either a full page or a fragment from the same handler.
  • Use htmx 4 attributes, request headers and response headers to update parts of a page.
  • Validate forms on the server and return errors with status codes htmx and plain browsers both handle.
  • Store data in SQLite through database/sql with contexts and timeouts.
  • Test handlers with httptest and ship the app as one binary with embedded assets.

lessons8, in order

  1. The hypermedia approach and a first server9 minWhat the server sends and what the browser does when HTML is the wire format, compared with a JSON API, and a first net/http server.
  2. Routing and handlers with the standard library22 minServeMux method and wildcard patterns, handlers as methods on an app struct, middleware as plain functions, and request logging with log/slog.
  3. Rendering HTML with templ12 mintempl components, a layout with children, automatic escaping, the generate step, and rendering a page or a single component from a handler.
  4. First htmx interactions14 minHow htmx 4 triggers, targets and swaps a request, how the server tells a fragment request from a page request, and three interactions built on that.
  5. Forms, validation and errors16 minServer-side validation, re-rendering a form with messages, how htmx 4 treats status codes, and redirect-after-post for clients without htmx.
  6. Persistence with SQLite16 mindatabase/sql with a pure-Go SQLite driver: schema setup, a store behind the same methods, mapping database errors, and contexts with deadlines.
  7. Interaction patterns18 minActive search with debounce, editing in place, delete with confirmation, a counter updated out of band, and a little Alpine.js.
  8. Testing and shipping18 minHandler and fragment tests with httptest, static files embedded in the binary, cross-origin protection, graceful shutdown, and a single file to deploy.

keyboard

h
home
w c a
writing, courses, about
n p
next and previous lesson
j k
scroll down and up
g G
top and bottom of the page
t
switch light and dark
?
this help

shortcuts are