获取Get请求

// Get 请求传值
    r.GET("/", func(c *gin.Context) {
        username := c.Query("username")
        age := c.Query("age")
        page := c.DefaultQuery("page", "1")
        //http://localhost:8080/?username=zangs&age=20
        c.JSON(http.StatusOK, gin.H{
            "username": username,
            "age":      age,
            "page":     page,
        })
    })

    //http://localhost:8080/article?id=2
    r.GET("/article", func(c *gin.Context) {
        id := c.DefaultQuery("id", "1")
        c.JSON(http.StatusOK, gin.H{
            "msg": "新闻详情",
            "id":  id,
        })
    })

获取Post请求

{{define "default/user.html"}}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <form action="/doAddUser" method="post">
        用户名:<input type="text" name="username"/> <br><br>
        密码:<input type="password" name="password"/> <br><br>
        年龄:<input type="text" name="age"/> <br><br>
        <input type="submit" value="提交">
    </form>
</body>
</html>
{{end}}

    //Post请求
    r.GET("/user", func(c *gin.Context) {
        c.HTML(http.StatusOK, "default/user.html", gin.H{})
    })

    r.POST("/doAddUser", func(c *gin.Context) {
        username := c.PostForm("username")
        password := c.PostForm("password")
        age := c.DefaultPostForm("age", "20")

        c.JSON(http.StatusOK, gin.H{
            "username": username,
            "password": password,
            "age":      age,
        })
    })

获取GET POST传递的数据绑定到结构体

    r.GET("/user", func(c *gin.Context) {
        c.HTML(http.StatusOK, "default/user.html", gin.H{})
    })
    //http://localhost:8080/getUser?username=gsy&password=jiyi
    r.GET("/getUser", func(c *gin.Context) {
        user := &UserInfo{}
        if err := c.ShouldBind(&user); err == nil {
            fmt.Printf("%#v", user)
            c.JSON(http.StatusOK, user)
        } else {
            c.JSON(http.StatusOK, gin.H{
                "err": err.Error(),
            })
        }
    })
    r.POST("/doAddUser1", func(c *gin.Context) {
        user := &UserInfo{}
        if err := c.ShouldBind(&user); err == nil {
            fmt.Printf("%#v", user)
            c.JSON(http.StatusOK, user)
        } else {
            c.JSON(http.StatusOK, gin.H{
                "err": err.Error(),
            })
        }
    })

获取 Post Xml数据

//获取 Post Xml数据
    r.POST("/xml", func(c *gin.Context) {
        article := &Article{}

        xmlSliceData, _ := c.GetRawData() //获取c.Request.Body 读取请求资料

        fmt.Println(&xmlSliceData)
        if err := xml.Unmarshal(xmlSliceData, &article); err == nil {
            c.JSON(http.StatusOK, article)
        } else {
            c.JSON(http.StatusBadRequest, gin.H{
                "err": err.Error(),
            })
        }
    })
<?xml version = "1.0" encoding = "UTF-8"?>
<article>
    <content type = "string">我是gsy</content>
    <title type = "string">jiyi</title>
</article>

动态路由传值

    r.GET("/list/:cid", func(c *gin.Context) {
        cid := c.Param("cid")
        c.String(200, "%v", cid)
    })

Gin路由分离

import (
    "fmt"
    "gindemo06/routers"
    "html/template"
    "time"
    "github.com/gin-gonic/gin"
)

func main() {
    // 创建一个默认的路由引擎
    r := gin.Default()
    //自定义模板函数  注意要把这个函数放在加载模板前
    r.SetFuncMap(template.FuncMap{
        "UnixToTime": UnixToTime,
    })
    //加载模板 放在配置路由前面
    r.LoadHTMLGlob("templates/**/*")
    //配置静态web目录   第一个参数表示路由, 第二个参数表示映射的目录
    r.Static("/static", "./static")

    routers.AdminRoutersInit(r)

    routers.ApiRoutersInit(r)

    routers.DefaultRoutersInit(r)

    r.Run()
}
package routers

import "github.com/gin-gonic/gin"

func apiRoutersInit(r *gin.Engine) {
    defaultRouters := r.Group("/")
    {
        defaultRouters.GET("/", func(c *gin.Context) {
            c.String(200, "首页")
        })
        defaultRouters.GET("/news", func(c *gin.Context) {
            c.String(200, "新闻")
        })
    }
}