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:
- A client requests
GET /comment. At this point,nameandmsgflash messages are empty. - A client requests
POST /commentsuch as through a form submission. - POST handler will set
nameandmsgflash messages, then returns a redirect response. - A client requests
GET /commentaccording to a redirect response.nameandmsghave been set by the previous POST handler. - When client will request
GET /commentagain,nameandmsgare empty because they have been get in the previous GET handler.