Skip to content

typelate/muxt

Repository files navigation

Muxt Go Reference Go

Server-rendered HTML, type-checked at go generate time.

{{define "GET /article/{id} GetArticle(ctx, id)"}}
  <h1>{{.Result.Title}}</h1>
{{end}}
func (s Server) GetArticle(ctx context.Context, id int) (Article, error) { ... }

The template name is the route, the handler call, and the parameter list. Muxt uses go/types to verify the whole chain — GetArticle exists on Server, id parses to int, .Result.Title is valid on Article — then writes the http.Handler glue. Typos and signature drift become go generate errors, not 5 PM pages.

Why Muxt

  • Single source of truth. Route, handler, and HTML live together. No separate mux.HandleFunc registration to drift out of sync.
  • Caught at generate time. go/types flags stale field access, parameter mismatches, and missing methods before they ship.
  • No runtime reflection. Generated code uses only net/http and html/template. Reads like hand-written Go.
  • Built for hypermedia. HTMX, Datastar, and plain server-rendered HTML are the happy path — not an afterthought.

Install

go install github.com/typelate/muxt@latest

Or as a project tool: go get -tool github.com/typelate/muxt (note the license). Pre-built binaries are also attached to each release.

Quick Start

  1. Create a template index.gohtml:

    {{define "GET / Home(ctx)"}}
    <h1>{{.Result}}</h1>
    {{end}}
  2. Wire it up in main.go:

    //go:embed *.gohtml
    var templateFS embed.FS
    
    //go:generate muxt generate --use-receiver-type=Server
    var templates = template.Must(template.ParseFS(templateFS, "*.gohtml"))
    
    type Server struct{}
    
    func (s Server) Home(ctx context.Context) string { return "Hello, Muxt!" }
  3. Generate and run:

    go generate && go run .

The templates variable must be package-level — Muxt finds it via static analysis.

Template Syntax

Standard http.ServeMux pattern, optionally with a status code and method call:

[METHOD ][HOST]/[PATH][ HTTP_STATUS][ CALL]

Example: "POST /user/{id} 201 CreateUser(ctx, id, form)"

Supported parameters: ctx, request, response, path params, form (URL-encoded body), multipart (file uploads, including *multipart.FileHeader fields). Returns and errors flow through TemplateData[R, T]. Status codes can come from the template name, return values, or error types.

TemplateRoutePaths extends type safety to URLs: {{$.Path.GetArticle 42}} instead of hardcoded href="/article/42". Change the route pattern, the compiler finds every stale reference.

Commands

  • muxt generate — generate http.Handler glue (writes template_routes.go)
  • muxt check — type-check templates without generating (use in CI or editor save hooks)
  • muxt list-template-calls / muxt list-template-callers — explore call sites and callers

Examples

The command tests double as readable examples of every feature.

Documentation

  • Reference — CLI, syntax, parameters, type checking
  • Explanation — design philosophy, patterns, decisions

See the full documentation index.

Go Standard Library

Using with AI Assistants

Claude Code skills for working with Muxt codebases:

Skill Use Case
explore-from-route Trace from a URL path to its template and receiver method
explore-from-method Find which routes and templates use a receiver method
explore-from-error Trace an error message back to its handler and template
explore-repo-overview Map all routes, templates, and the receiver type
test-driven-development Create new templates and receiver methods using TDD
forms Form creation, struct binding, validation, accessible HTML
debug-generation-errors Diagnose and fix muxt generate / muxt check errors
refactoring Rename methods, change patterns, move templates safely
htmx Explore, develop, and test HTMX interactions
integrate-existing-project Add Muxt to an existing Go web application
sqlc Use Muxt with sqlc for type-safe SQL + HTML
goland-gotype Add gotype comments for GoLand IDE support (GoLand-only)
maintain-tools Install and update muxt, gofumpt, counterfeiter, and other tools

Install as Claude Code skills:

for d in docs/skills/*/; do cp -r "$d" ~/.claude/skills/"$(basename "$d")"; done

License

Muxt generator: GNU AGPLv3

Generated code: not covered by AGPL. Muxt asserts no copyright over its output — treat generated files as your own code, under whatever license your project uses.

No warranty. Muxt and its generated output are provided "as is", without warranty of any kind, express or implied, including but not limited to warranties of merchantability, fitness for a particular purpose, and non-infringement. You are responsible for reviewing, testing, and securing any code generated by Muxt before using it. In no event shall the authors or copyright holders be liable for any claim, damages, or other liability arising from the use of Muxt or its output.