Guestbook (Plain)
A guestbook using traditional HTML forms and the Post/Redirect/Get pattern. No JavaScript, no libraries — just a <form> with method="POST".
If you can read, you can cook. If you know Go, you can use Gastro.
I don't really know what I'm doing, but the file-based routing makes it easy.
Anyone can cook... and anyone can build web apps with Gastro!
How It Works
This is the simplest way to handle user input in Gastro. The .gastro page renders the form (GET), and a plain Go handler in main.go processes the submission (POST).
The Page
The page renders the form and reads query parameters for success/error messages via ctx.Query():
---
ctx := gastro.Context()
Entries := demo.ListEntries()
Error := ctx.Query("error")
Success := ctx.Query("success")
---
{{ range .Entries }}
{{ GuestbookEntry (dict "ID" .ID ...) }}
{{ end }}
<form method="POST" action="/guestbook">
{{ GuestbookForm (dict "Error" .Error "Success" .Success) }}
<button type="submit">Sign Guestbook</button>
</form>
The POST Handler
The POST handler lives in main.go (since Gastro only generates GET routes). It validates input, adds the entry, and redirects back using the Post/Redirect/Get (opens in new tab) pattern:
// In main.go
mux.HandleFunc("POST /guestbook", handleGuestbookPost)
func handleGuestbookPost(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
name := strings.TrimSpace(r.FormValue("name"))
message := strings.TrimSpace(r.FormValue("message"))
if name == "" {
http.Redirect(w, r,
"/examples/guestbook-plain?error=Name+is+required",
http.StatusSeeOther)
return
}
demo.AddEntry(name, message)
http.Redirect(w, r,
"/examples/guestbook-plain?success=Entry+added",
http.StatusSeeOther)
}
Progressive Enhancement
This same form can be enhanced with Datastar or HTMX to avoid full-page reloads. The only changes are HTML attributes — no JavaScript needed:
<!-- Before: traditional form -->
<form method="POST" action="/guestbook">
<!-- After: Datastar-enhanced -->
<form data-on:submit="@post('/api/ds/add')"
data-indicator:submitting>
<!-- Or: HTMX-enhanced -->
<form hx-post="/api/htmx/add"
hx-target="#guestbook-list"
hx-swap="outerHTML">
See the Datastar and HTMX examples for these patterns in action.