• 3.Gin 框架中的路由简要说明


    3.Gin 框架中的路由简要说明

    Gin 框架中的路由

    路由概述

    路由(Routing)是由一个 URI(或者叫路径)和一个特定的 HTTP 方法(GET、POST 等)

    组成的,涉及到应用如何响应客户端对某个网站节点的访问。

    RESTful API 是目前比较成熟的一套互联网应用程序的 API 设计理论,所以我们设计我们的路

    由的时候建议参考 RESTful API 指南。

    在 RESTful 架构中,每个网址代表一种资源,不同的请求方式表示执行不同的操作:

    GET(SELECT)从服务器取出资源(一项或多项)
    POST(CREATE)在服务器新建一个资源
    PUT(UPDATE)在服务器更新资源(客户端提供改变后的完整资源)
    DELETE(DELETE)从服务器删除资源

    简单的路由配置

    简单的路由配置
    GET 请求示例
    66c5ec8fa9a9c479018c121864dec8f6.png
    1695656801157
    1. // GET 请求示例
    2. r.GET("/hello"func(c *gin.Context) {
    3.     c.JSON(200, gin.H{
    4.        "message""hello....",
    5.     })
    6. })

    测试如下:

    curl -X GET  http://localhost:8000/hello
    290f4411ec3ff28228c0bb0fcce98a50.png
    1695657576419
    POST请求示例
    1. // POST 请求示例
    2. r.POST("/add"func(c *gin.Context) {
    3.     c.JSON(http.StatusOK, gin.H{
    4.        "message""add",
    5.     })
    6. })

    测试如下:

    curl -X POST -H "Content-Type: application/json"  http://localhost:8000/add
    93b167c94fd5734d1c84d93846608d27.png
    1695657740216
    PUT请求示例
    1. // PUT 请求示例
    2. r.PUT("/put"func(c *gin.Context) {
    3.     c.JSON(http.StatusOK, gin.H{
    4.        "message""put",
    5.     })
    6. })

    测试如下:

    curl -X PUT -H "Content-Type: application/json"  http://localhost:8000/put
    0752c2fa2418b26117148d1e2e5c7acb.png
    1695658030962
    DELETE请求
    1. // DETELE 请求示例
    2. r.DELETE("/delete"func(c *gin.Context) {
    3.     c.JSON(http.StatusOK, gin.H{
    4.        "message""delete",
    5.     })
    6. })

    测试如下:

    curl -X DELETE -H "Content-Type: application/json"  http://localhost:8000/delete
    f92012e54f7253dda891840d2a704615.png
    1695658161530
    获取query参数、路径参数
    路由里面获取 Query 参数
    1. // GET 获取query参数示例
    2. r.GET("/user"func(c *gin.Context) {
    3.     // 获取query参数
    4.     uid := c.Query("uid")
    5.     // 返回响应信息
    6.     c.JSON(http.StatusOK, gin.H{
    7.        "message": fmt.Sprintf("uid=%s", uid),
    8.     })
    9. })

    测试如下:

    curl -X GET  http://localhost:8000/user?uid=test001
    c123d2985c09d5051735f701133fd54f.png
    1695660194092

    获取动态路由的路径参数

    1. // GET 获取path路径参数
    2. r.GET("/book/:bid"func(c *gin.Context) {
    3.     // 获取path参数
    4.     bid := c.Param("bid")
    5.     // 返回响应信息
    6.     c.JSON(http.StatusOK, gin.H{
    7.        "message": fmt.Sprintf("bid=%s", bid),
    8.     })
    9. })

    测试 如下:

    curl -X GET  http://localhost:8000/book/bid001
    b06c5ab59fcf4fd53899b73844ad8ef9.png
    1695660700658
    路由响应数据:c.String() c.JSON() c.JSONP() c.XML() c.HTML()
    c.String() 返回一个字符串

    在上面的代码示例中,其实已经使用过了。

    1. // c.String 响应内容为字符串
    2. r.GET("/news"func(c *gin.Context) {
    3.     // 获取参数
    4.     aid := c.Query("aid")
    5.     // 返回字符串
    6.     c.String(http.StatusOK, "aid=%s", aid)
    7. })

    测试如下:

    curl -X GET  http://localhost:8000/news?aid=aid001
    01a7a92eaeaafa3f0eb7b6eb176cd45a.png
    1695740680020
    c.JSON() 返回 JSON 字符串
    1. r.GET("/json1"func(c *gin.Context) {
    2.     // 使用 map[string]any  返回json内容
    3.     c.JSON(200map[string]any{
    4.        "code"0,
    5.        "data""json1",
    6.     })
    7. })
    8. r.GET("/json2"func(c *gin.Context) {
    9.     // gin.H 是 map[string]any的缩写
    10.     c.JSON(200, gin.H{
    11.        "code"0,
    12.        "data""json2",
    13.     })
    14. })
    15. r.GET("/json3"func(c *gin.Context) {
    16.     // 使用结构体设置JSON结构
    17.     var msg struct {
    18.        Name    string `json:"user"` // 使用`json:"user"` 定义json字符串返回的别名,例如Name在json字符串中为user
    19.        Message string `json:"message"`
    20.        Age     int    `json:"age"`
    21.     }
    22.     msg.Name = "lijw"
    23.     msg.Message = "Hello world!"
    24.     msg.Age = 18
    25.     // 使用结构体对象,返回JSON数据
    26.     c.JSON(http.StatusOK, msg)
    27. })

    测试如下:

    1. curl -X GET  http://localhost:8000/json1
    2. curl -X GET  http://localhost:8000/json2
    3. curl -X GET  http://localhost:8000/json3
    f8c6e17b0f31d5090ecb27694b3745b5.png
    1695741515412
    c.JSONP() 返回回调JSON
    1. // c.JSONP() 返回回调JSON
    2. r.GET("/JSONP"func(c *gin.Context) {
    3.     data := map[string]interface{}{ "foo""bar", }
    4.     // /JSONP?callback=x
    5.     // 将输出:x({\"foo\":\"bar\"})
    6.     c.JSONP(http.StatusOK, data)
    7. })

    测试如下:

    1. curl -X GET  http://localhost:8000/JSONP
    2. curl -X GET  http://localhost:8000/JSONP?callback=x
    a71213fcb1fa2916e5b04b7cc99930e7.png
    1695741865466
    c.XML() 返回 XML 数据
    1. // c.XML() 返回 XML 数据
    2. r.GET("/xml1"func(c *gin.Context) {
    3.     // 方式一:手动拼接JSON
    4.     c.XML(http.StatusOK, gin.H{
    5.        "code"0,
    6.        "data""xml1",
    7.     })
    8. })
    9. r.GET("/xml2"func(c *gin.Context) {
    10.     // 方法二:使用结构体
    11.     type MessageRecord struct {
    12.        Name    string `xml:"name"`
    13.        Message string `xml:"data"`
    14.        Age     int    `xml:"age"`
    15.     }
    16.     var msg MessageRecord
    17.     msg.Name = "lijw"
    18.     msg.Message = "Hello world!"
    19.     msg.Age = 18
    20.     // 返回XML格式内容
    21.     c.XML(http.StatusOK, msg)
    22. })

    测试如下:

    http://localhost:8000/xml1

    519e9545ee42158ebb3f5f01e1b841ae.png
    1695742445725

    http://localhost:8000/xml2

    ff6e21dc97e6e08e994b3c9f4ccc776c.png
    1695742466927
    c.HTML 渲染模板
    创建用于渲染的模板html
    a79c7a80fc50251fef24bd56264f4d13.png
    1695745504219

    templates/index.html

    1. "en">
    2.     "UTF-8">
    3.     Title
    4.     

      index

    5.     

      渲染的内容: {{.title}}

    templates/goods.html

    1. "en">
    2.     "UTF-8">
    3.     Title
    4.     

      商品页面

    5.     

      渲染的内容: {{.title}}

    路由加载模板文件
    af5bffe8ff9566536df6f9aece708602.png
    1695745561929
    1. // 加载模板文件
    2. r.LoadHTMLGlob("templates/*")
    渲染模板
    1. // c.HTML 渲染模板
    2. r.GET("/index"func(c *gin.Context) {
    3.     c.HTML(http.StatusOK, "index.html", gin.H{"title""前台首页"})
    4. })
    5. r.GET("/goods"func(c *gin.Context) {
    6.     c.HTML(http.StatusOK, "goods.html", gin.H{"title""商品页面"})
    7. })
    测试如下

    访问 http://localhost:8000/index

    fe8c882e1899a22b1ef872a649900762.png
    1695745687350

    访问 http://localhost:8000/goods

    9260b679be18fcf8c21b0693487242c7.png
    1695745711391
  • 相关阅读:
    Linux下gcc编译器和gdb调试
    面向对象设计模式
    云原生爱好者周刊:macOS 秒级启动虚拟机 | 2022-08-22
    分库分表实现方式Client和Proxy,性能和维护性该怎么选?
    汇聚荣拼多多运营策略是怎么样的?
    SDK各类型广告的效果
    DW大学生网页作业制作设计 中华饮食文化(HTML+CSS+JavaScript) Web前端大作业
    QCC51XX---串口仿真协议( RFCOMM)
    关于为了少搬砖,而用node手写了一个React脚手架这件事
    实现高并发秒杀的七种方式 !
  • 原文地址:https://blog.csdn.net/u012887259/article/details/134522421