Gin框架渲染

JSON数据解析和绑定 ShouldBindJSON

客户端传参,后端解析绑定

package main

import (
  "github.com/gin-gonic/gin"
  "net/http"
)

type Login struct {
  UserName string `form:"username" json:"username" uri:"username" xml:"username" binding:"required"`
  Password string `form:"password" json:"password" uri:"password"  xml:"password" binding:"required"`
}

func main() {
   //创建路由
  r := gin.Default()
   //创建POST请求
  r.POST("/login", func(context *gin.Context) {
      var login Login
       //把body映射到结构体里面取
      if err := context.ShouldBindJSON(&login); err != nil {
          context.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
          return
      }
      if login.UserName != "root" || login.Password != "admin" {
          context.JSON(http.StatusBadRequest, gin.H{"status": "304"})
          return
      }
       //返回json格式
      context.JSON(http.StatusOK, gin.H{"status": 200})
  })
  r.Run(":8088")
}

postman进行测试

//request
{
"username":"root",
"password":"admin"
}
//result
{
   "status": 200
}

表单数据解析和绑定 Bind()

package main

import (
  "github.com/gin-gonic/gin"
  "net/http"
)

type Login struct {
  UserName string `form:"username" json:"username" uri:"username" xml:"username" binding:"required"`
  Password string `form:"password" json:"password" uri:"password"  xml:"password" binding:"required"`
}

func main() {
    //创建路由
  r := gin.Default()
    //创建POST请求
  r.POST("/login", func(context *gin.Context) {
      var form Login
        //Bind默认解析并绑定form格式
      if err := context.Bind(&login); err != nil {
          context.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
          return
      }
      if form.UserName != "root" || form.Password != "admin" {
          context.JSON(http.StatusBadRequest, gin.H{"status": "304"})
          return
      }
        //返回json格式
      context.JSON(http.StatusOK, gin.H{"status": 200})
  })
  r.Run(":8088")
}

URL数据解析和绑定

package main

import (
  "github.com/gin-gonic/gin"
  "net/http"
)

type Login struct {
  UserName string `form:"username" json:"username" uri:"username" xml:"username" binding:"required"`
  Password string `form:"password" json:"password" uri:"password"  xml:"password" binding:"required"`
}

func main() {
    //创建路由
  r := gin.Default()
    //创建POST请求
  r.POST("/login", func(context *gin.Context) {
      var form Login
        //ShouldBindUri绑定URL格式
      if err := context.ShouldBindUri(&login); err != nil {
          context.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
          return
      }
      if form.UserName != "root" || form.Password != "admin" {
          context.JSON(http.StatusBadRequest, gin.H{"status": "304"})
          return
      }
        //返回json格式
      context.JSON(http.StatusOK, gin.H{"status": 200})
  })
  r.Run(":8088")
}

响应格式

c.JSON()//json格式响应
c.XML()//XML格式响应
c.YAML()//YAML格式响应
c.ProtoBuf()//ProtoBuf 谷歌开发的高效存储读取工具

同步异步

goroutine 机制可以方便的实现异步处理。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • https://learnku.com/docs/gin-gonic/2018/gin-readme/3819 安...
    程序员的自我修养阅读 1,210评论 0 1
  • 转发自:http://shanshanpt.github.io/2016/05/03/go-gin.html gi...
    dncmn阅读 6,078评论 0 1
  • 1.方法一:本地启动服务,用浏览器或者postman测试,但是项目有改动再次测试不是很方便2.方法二:使用http...
    bug去无踪阅读 3,313评论 0 0
  • 所谓框架 框架一直是敏捷开发中的利器,能让开发者很快的上手并做出应用,甚至有的时候,脱离了框架,一些开发者都不会写...
    人世间阅读 216,423评论 11 242
  • 我是黑夜里大雨纷飞的人啊 1 “又到一年六月,有人笑有人哭,有人欢乐有人忧愁,有人惊喜有人失落,有的觉得收获满满有...
    陌忘宇阅读 8,594评论 28 53