Page:
Return
Pages
Actions
Basicauth
Binding
Captcha
Compress
Context
Debug
Dispatch
ErrHandler
Events
Flash
Forms
Group
Handler
Home
Injection
Logger
Params
QuickStart
Recovery
Renders
Return
Router
Session
Static
Tango
Tpongo2
Xsrf
ZH_Actions
ZH_Basicauth
ZH_Binding
ZH_Captcha
ZH_Compress
ZH_Context
ZH_Debug
ZH_Dispatch
ZH_ErrHandler
ZH_Events
ZH_Flash
ZH_Forms
ZH_Group
ZH_HOME
ZH_Handler
ZH_Injection
ZH_Logger
ZH_Params
ZH_Recovery
ZH_Renders
ZH_Return
ZH_Router
ZH_Session
ZH_Static
ZH_Tango
ZH_Tpongo2
ZH_Xsrf
3
Return
Lunny Xiao edited this page 2017-04-11 14:52:23 +08:00
Table of Contents
Return Values
According func or method's return values, returnHandle will automatically write content to ResponseWriter.
The return value could be below types or interfaces:
-
string
Will convert string to []byte and write to ResponseWriter -
[]byte
Write to ResponseWriter -
error
If error is not nil, Write header 500 and the content is error.Error() -
AbortError
if AbortError, Write header AbortError.Code and the content is AbortError.Error()
And if you have an anonymous tango.JSON
or tango.XML
, the return value could be:
-
error
If it's Json, will reproduce {"err": err.Error()} -
map
If it's Json, will reproduce json -
slice
-
structs
will reproduce json
For example, we will write a json response.
type Action struct {
tango.JSON
}
var i int
func (Action) Get() interface{} {
if i == 0 {
i = i + 1
return map[string]interface{}{"i":i}
}
return errors.New("could not visit")
}
func main() {
t := tango.Classic()
t.Any("/", new(Action))
t.Run()
}