๐ค flash
amadan.net/rastrillo/rastrillo/flash
One-shot notice messages carried in a cookie and cleared once read.
A flash is display state, not a record. Losing one to a cleared cookie costs a notice, not data, which is what makes a cookie the right place for it instead of a database table with a lifecycle to manage.
Flash
type Flash struct {
Kind string
Message string
}
Kind is your own vocabulary โ "notice", "error" โ and your layout
decides how each renders.
Set
func Set(w http.ResponseWriter, r *http.Request, kind, message string)
Writes the cookie. It takes the request because a flash belongs to the app that set it: at the root the cookie is rastrillo_flash at Path=/, and under a base path it is rastrillo_flash_<slug> at Path=/<slug>. Call it immediately before the redirect that will show it:
flash.Set(w, r, "notice", "Note created.")
http.Redirect(w, r, "/notes/"+id, http.StatusSeeOther)
Take
func Take(w http.ResponseWriter, r *http.Request) (Flash, bool)
Reads and clears in one call, which is the one-shot part. It reads only the cookie for the request's own base path, so one app never shows or clears another's notice on the same origin. Call it once per page from your render helper and let the layout render what it returns; call it twice in one request and the second caller gets nothing.
A missing or unparseable cookie answers false with a zero Flash
instead of an error. There is nothing useful an app could do about a
corrupt notice cookie except ignore it.
Read this page as markdown โ exact, unstyled, and cheap for an agent to fetch.