Flash

Flash is for the one-time messaging between requests. It useful for
implementing the Post/Redirect/Get pattern.
This feature is provided by FlashMiddleware.

Basics

For example, when controller is below and route /comment is set to it,

func (r *Root) GET(c *kocha.Context) error {
    name := c.Flash.Get("name")
    msg := c.Flash.Get("msg")
    return c.Render(map[string]interface{}{
        "name": name,
        "msg": msg,
    })
}

func (r *Root) POST(c *kocha.Context) error {
    c.Flash.Set("name", "alice")
    c.Flash.Set("msg", "your comment has been posted!")
    // do something...
    return c.Redirect("/comment", false)
}

Sequence flow:

  1. A client requests GET /comment. At this point, name and msg flash messages are empty.
  2. A client requests POST /comment such as through a form submission.
  3. POST handler will set name and msg flash messages, then returns a redirect response.
  4. A client requests GET /comment according to a redirect response. name and msg have been set by the previous POST handler.
  5. When client will request GET /comment again, name and msg are empty because they have been get in the previous GET handler.