3

If we want to get the information on the URI, such as the bill and 17 of http://127.0.0.1:9090/bill/17 , we can write:

 package main

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

func main() {
    r := gin.Default()
    r.GET("/:name/:age", func(c *gin.Context) {
        //    获取路径参数
        name := c.Param("name")
        age := c.Param("age")
        c.JSON(http.StatusOK, gin.H{
            "name": name,
            "age":  age,
        })
    })
    r.Run(":9090")
}

The effect is as follows:


Reference: bilibili

LiberHome
409 声望1.1k 粉丝

有问题 欢迎发邮件 📩 liberhome@163.com