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,name
andmsg
flash messages are empty. - A client requests
POST /comment
such as through a form submission. - POST handler will set
name
andmsg
flash messages, then returns a redirect response. - A client requests
GET /comment
according to a redirect response.name
andmsg
have been set by the previous POST handler. - When client will request
GET /comment
again,name
andmsg
are empty because they have been get in the previous GET handler.