golang实战使用gin+xorm搭建go语言web框架restgo详解5.7 控制器数据响应

数据响应包含俩个方面的内容,一是数据格式的封装,一是数据响应类型.

先说数据响格式封装,为了别于运维,一般我们需要将响应结果封装到函数里面,这样前端获取的数据结构统一。对于api来说,响应到前端的数据包含如下几个参数

l code:用于指示数据请求状态,200表示成功

l data:后端服务返回的基础数据,一般是对象

l msg:后端调用返回的操作提示,如恭喜你操作成功或者失败则提示失败原因

l rows:当后端返回是数组时,该参数代表获取的全部记录,该参数可以适配jquerytable系列前端框架

l total:当返回数组时,该参数用来标识全部记录数目

restgo/Result.go中我们封装了4个函数如下

//对于基础响应函数,需要制定返回的code
func Result(ctx * gin.Context,code int,data interface{},msg string){
   ctx.JSON(http.StatusOK, gin.H{"code": code, "data": data, "msg":msg})
}
//请求成功响应函数,携带数据
func ResultOk(ctx * gin.Context,data interface{}){
   ctx.JSON(http.StatusOK, gin.H{"code": http.StatusOK, "data": data, "msg": ""})
}
//请求列表成功响应函数,携带数据列表和总页数
func ResultList(ctx * gin.Context,data interface{},total int64){
   ctx.JSON(http.StatusOK, gin.H{"code": http.StatusOK, "rows": data, "msg": "","total":total})
}
//请求成功响应提示,携带提示和数据
func ResultOkMsg(ctx * gin.Context,data interface{},msg string){
   ctx.JSON(http.StatusOK, gin.H{"code": http.StatusOK, "data": data, "msg": msg})
}
//请求失败响应函数,携带失败原因
func ResultFail(ctx * gin.Context,err interface{}){
   ctx.JSON(http.StatusOK, gin.H{"code": http.StatusBadRequest, "data": nil, "msg":err})
}
//请求失败响应函数,携带失败原因和数据
func ResultFailData(ctx * gin.Context,data interface{},err interface{}){
   ctx.JSON(http.StatusOK, gin.H{"code": http.StatusBadRequest, "data": data, "msg":err})
}

数据类型方面,gin框架内置了各种数据类型响应格式,包括xml\json\yaml\html

1、 xml格式

func main() {
   r := gin.Default()
   r.GET("/someXML", func(c *gin.Context) {
      c.XML(http.StatusOK, gin.H{"message": "hey", "status": http.StatusOK})
   })
   // Listen and serve on 0.0.0.0:8080
   r.Run(":8080")
}

2、 json格式

func main() {
   r := gin.Default()
   r.GET("/moreJSON", func(c *gin.Context) {
   // You also can use a struct
   var msg struct {
      Name    string `json:"user"`
      Message string
      Number  int
   }
   msg.Name = "Lena"
   msg.Message = "hey"
   msg.Number = 123
   // Note that msg.Name becomes "user" in the JSON
   // Will output  :   {"user": "Lena", "Message": "hey", "Number": 123}
   c.JSON(http.StatusOK, msg)

})

// Listen and serve on 0.0.0.0:8080

r.Run(":8080")}

3、 yaml格式

func main() {
   r := gin.Default()
  r.GET("/someYAML", func(c *gin.Context) {
   c.YAML(http.StatusOK, gin.H{"message": "hey", "status": http.StatusOK})

})  // Listen and serve on 0.0.0.0:8080

 r.Run(":8080")

}

4、 静态资源文件

func main() {
   r := gin.Default()
  r.StaticFile("/favicon.ico", "./resources/favicon.ico")   // Listen and serve on 0.0.0.0:8080
   r.Run(":8080")
}

5、 html文件

func main() {
   router := gin.Default()
   router.LoadHTMLGlob("templates/*")
   //router.LoadHTMLFiles("templates/template1.html", "templates/template2.html")
   router.GET("/index", func(c *gin.Context) {
      c.HTML(http.StatusOK, "index.tmpl", gin.H{
         "title": "Main website",
      })
   })
   router.Run(":8080")
}
view/index.html
{{templete  "index.html"}}
<html>
<h1>
{{ .title }}
</h1>
</html>

{{end}}

5.8 异常处理

异常处理在路由章节以及做了详细描述,这里不做详细阐述。

源代码获取地址 http://www.imwinlion.com

待提供源代码清单

10.1 restgo后台管理框架

10.天天任务清单小程序

10.工业大数据采集

10.restgo cms 

10.restgo 千人大群

golang实战使用gin+xorm搭建go语言web框架restgo详解1.1 go语言的困境

golang实战使用gin+xorm搭建go语言web框架restgo详解1.2 我要做什么

golang实战使用gin+xorm搭建go语言web框架restgo详解2 框架基本架构

golang实战使用gin+xorm搭建go语言web框架restgo详解3 系统常用配置参数

golang实战使用gin+xorm搭建go语言web框架restgo详解4 路由配置

golang实战使用gin+xorm搭建go语言web框架restgo详解5 控制器C

golang实战使用gin+xorm搭建go语言web框架restgo详解5.2 跳转和重定向

golang实战使用gin+xorm搭建go语言web框架restgo详解5.3 资源控制器

golang实战使用gin+xorm搭建go语言web框架restgo详解5.4 控制器参数绑定

golang实战使用gin+xorm搭建go语言web框架restgo详解5.5 控制器模型绑定

golang实战使用gin+xorm 搭建 go语言web框架restgo搭建详解5.6 控制器参数校验

Golang go语言整合gin+xorm 搭建 web框架restgo搭建详解5.7 控制器数据响应

golang实战使用gin+xorm搭建go语言web框架restgo详解5.9 控制器controller编程

golang实战使用gin+xorm搭建go语言web框架restgo详解6.1 模型M和Orm

golang实战使用gin+xorm搭建go语言web框架restgo详解6.4 推荐编程方式

golang实战使用gin+xorm搭建go语言web框架restgo详解7 视图层V

golang实战使用gin+xorm搭建go语言web框架restgo详解8 关于模板

golang实战使用gin+xorm搭建go语言web框架restgo详解9 session、日志、鉴权

作者简介:胡文林,持续创业者,长期从事技术开源工作。微信号jiepool-winlion

猜你喜欢

转载自blog.csdn.net/keytounix/article/details/79336597