我们可以使用下面的代码来打印出 gin 所有路由:
func main() {
router := gin.Default()
router.GET("/hello", func(c *gin.Context) {
c.String(http.StatusOK, "Hello, world!")
})
router.POST("/login", func(c *gin.Context) {
username := c.PostForm("username")
password := c.PostForm("password")
c.JSON(http.StatusOK, gin.H{
"Username": username,
"Password": password,
})
})
router.GET("/users/:id", func(c *gin.Context) {
id := c.Param("id")
c.String(http.StatusOK, "User ID: %s", id)
})
// print all routes
for _, route := range router.Routes() {
fmt.Printf("%s %s\n", route.Method, route.Path)
}
router.Run(":8080")
}
在此代码中,我们使用 router.Routes()
函数来获取 gin 所有的路由,并通过循环遍历打印出每个路由的 HTTP 方法和路径。
评论(0)