教程 > gin 教程 > 阅读:91

gin 重定向——迹忆客-ag捕鱼王app官网

http重定向

package main
import (
    "github.com/gin-gonic/gin"
    "net/http"
)
func main() {
    r := gin.default()
    r.get("/test", func(c *gin.context) {
        c.redirect(http.statusmovedpermanently, "https://www.jiyik.com/")
    })
    r.run()
}

以上代码执行结果如下

gin 重定向


路由重定向

package main
import (
    "github.com/gin-gonic/gin"
    "net/http"
)
func main() {
    r := gin.default()
    r.get("/test1", func(c *gin.context) {
        // 指定重定向的url
        c.request.url.path = "/test2"
        r.handlecontext(c)
    })
    r.get("/test2", func(c *gin.context) {
        c.json(http.statusok, gin.h{"hello": "world"})
    })
    r.run()
}

以上代码执行结果如下

gin 路由重定向

查看笔记

扫码一下
查看教程更方便
网站地图