Page:
ZH_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
Table of Contents
This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
返回值
根据函数或者结构体方法的返回值,returnHandle 插件将自动将内容写入到 ResponseWriter. 目前支持的返回值及对应的行为如下:
-
string
返回string,将会把string转为[]byte同时写入到ResponseWriter -
[]byte
返回[]byte将会直接写入ResponseWriter -
error
返回错误,如果error不为nil, 则写入返回头500,内容为error.Error() -
AbortError
如果返回AbortError, 则写入返回头AbortError.Code,内容为AbortError.Error()
如果包含了匿名结构体tango.JSON
或者tango.XML
,则返回值的行为将会发生变化:
-
error
返回值为error,如果是Json,则会生成{"err": err.Error()}
的Json格式 -
map
,slice
,structs
返回值为map,如果是Json,则会自动序列化为Json格式。
例子代码如下:
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()
}