Using Success and Fail Methods
In addition to basic methods such as JSON
, Text
, and XML
, the Lightning
framework provides two convenient methods, Success
and Fail
, for standardizing HTTP response data formats.
package main
import (
"github.com/go-labx/lightning"
)
func main() {
app := lightning.DefaultApp()
app.Get("/success", func(ctx *lightning.Context) {
ctx.Success("hello world")
})
app.Get("/fail", func(ctx *lightning.Context) {
ctx.Fail(9999, "network error")
})
app.Run()
}
The Success
method returns data in the following format:
{
"code": 0,
"data": "hello world",
"message": "ok"
}
The Fail
method returns data in the following format:
{
"code": 9999,
"message": "network error"
}