flash

package
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 15, 2026 License: MIT Imports: 3 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package flash provides utilities for one-time flash messages using cookies. Flash messages are commonly used to show feedback after form submissions or redirects (e.g., "Registration successful!" or "Invalid credentials").

Index

Constants

View Source
const (
	// ErrorKey is the cookie name for error flash messages
	ErrorKey = "flash_error"
	// SuccessKey is the cookie name for success flash messages
	SuccessKey = "flash_success"
	// PendingKey marks that a flash message was just set (for one-time display)
	PendingKey = "flash_pending"

	// DefaultMaxAge is how long flash cookies live (30 seconds)
	// This allows time for slow redirects or network delays
	DefaultMaxAge = 30
)

Variables

This section is empty.

Functions

func ClearPending

func ClearPending(w http.ResponseWriter)

ClearPending clears the pending marker.

func Error

func Error(w http.ResponseWriter, message string)

Error sets an error flash message.

Example:

flash.Error(w, "Invalid email or password")

func Get

func Get(r *http.Request, w http.ResponseWriter, key string) string

Get reads and clears a flash message. Returns empty string if no flash message exists.

Example:

if msg := flash.Get(r, w, "error"); msg != "" {
    // Display error message
}

func GetError

func GetError(r *http.Request, w http.ResponseWriter) string

GetError reads and clears the error flash message.

Example:

if errMsg := flash.GetError(r, w); errMsg != "" {
    state.FlashError = errMsg
}

func GetSuccess

func GetSuccess(r *http.Request, w http.ResponseWriter) string

GetSuccess reads and clears the success flash message.

Example:

if successMsg := flash.GetSuccess(r, w); successMsg != "" {
    state.FlashSuccess = successMsg
}

func IsPending

func IsPending(r *http.Request) bool

IsPending checks if a flash message was just set (vs page reload).

func RedirectWithError

func RedirectWithError(w http.ResponseWriter, r *http.Request, redirectURL, message string)

RedirectWithError redirects to a URL with an error flash message.

Example:

flash.RedirectWithError(w, r, "/auth", "Invalid credentials")

func RedirectWithSuccess

func RedirectWithSuccess(w http.ResponseWriter, r *http.Request, redirectURL, message string)

RedirectWithSuccess redirects to a URL with a success flash message.

Example:

flash.RedirectWithSuccess(w, r, "/", "Welcome back!")

func Set

func Set(w http.ResponseWriter, key, message string)

Set sets a flash message cookie with the given key. The message is URL-encoded to handle special characters.

Example:

flash.Set(w, "info", "Your session will expire soon")

func SetPending

func SetPending(w http.ResponseWriter)

SetPending marks that a flash message was just set. This is used to distinguish between a fresh flash and a page reload.

func Success

func Success(w http.ResponseWriter, message string)

Success sets a success flash message.

Example:

flash.Success(w, "Account created successfully!")

Types

type Messages

type Messages struct {
	Error   string
	Success string
}

Messages holds both error and success flash messages. Useful for passing to templates.

func GetAll

func GetAll(r *http.Request, w http.ResponseWriter) Messages

GetAll reads and clears both error and success flash messages.

Example:

msgs := flash.GetAll(r, w)
state.FlashError = msgs.Error
state.FlashSuccess = msgs.Success

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL