HTTP重定向
package mainimport ("net/http""github.com/gin-gonic/gin")func main() {router := gin.Default()router.GET("/ssss", func(ctx *gin.Context) { //使用匿名函数ctx.Redirect(http.StatusMisdirectedRequest, "www.baidu.com")})router.Run(":9090")}
路由重定向
package mainimport ("net/http""github.com/gin-gonic/gin")func main() {router := gin.Default()router.GET("/path1", func(ctx *gin.Context) { //使用匿名函数ctx.Request.URL.Path = "/path2" //访问path1,就重定向到path2router.HandleContext(ctx) //})router.GET("/path2", func(ctx *gin.Context) {ctx.JSON(http.StatusOK, gin.H{"msg": "你被重定向到了path2",})})router.Run(":9090")}

路由组
当有/user/1,/user/2,/user/3,这样的路由的时候,可以将/user/作为一个路由组。
package mainimport ("github.com/gin-gonic/gin")func main() {router := gin.Default()rgroup := router.Group("/user") //定义路由组{rgroup.GET("/1", func(ctx *gin.Context) { ctx.JSON(200, gin.H{"msg": "这是1页面"}) })rgroup.GET("/2", func(ctx *gin.Context) { ctx.JSON(200, gin.H{"msg": "这是2页面"}) })rgroup.GET("/3", func(ctx *gin.Context) { ctx.JSON(200, gin.H{"msg": "这是3页面"}) })}router.Run(":9090")}


