api的url规则设计,带参数的路由

api的url设计规则

router := gin.Default()
router.GET("/topic/:topic_id", func(context *gin.Context) {
    context.String(http.StatusOK,"获取帖子Id为%s",context.Param("topic_id"))
})
router.Run()

package main

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

type Topic struct {
    TopicID int
}


func main() {

    router := gin.Default()
    router.GET("/topic/:topic_id", func(c *gin.Context) {
        if c.Query("username") == "" {
            c.String(200,"获取帖子列表")
        }else{
            c.String(200,"获取用户名=%s的帖子列表",c.Query("username"))
        }
    })
    router.GET("/v1/topics/:topic_id", func(c *gin.Context) {
        c.String(200,"获取topic=%s的帖子列表",c.Param("topic_id"))
    })
    router.Run()
}




猜你喜欢

转载自www.cnblogs.com/hualou/p/12071037.html