go 中 发送重定向非常容易,同时支持 内部和外部地址。
外部重定向使用 : content.Redirect
路由重定向使用:
a. 通过 content.Request.URL.Path 重新设置路由
b. 通过 router.HandleContent(content) 重调
1. 外部重定向
package main
import (
"github.com/gin-gonic/gin"
_ "github.com/jinzhu/gorm/dialects/mysql"
"net/http"
)
func main() {
r := gin.Default()
r.GET("/redirect", func(c *gin.Context) {
c.Redirect(http.StatusMovedPermanently,"http://www.baidu.com")
})
r.Run(":3000")
}
2. 内部路由重定向
package main
import (
"github.com/gin-gonic/gin"
_ "github.com/jinzhu/gorm/dialects/mysql"
"net/http"
)
func main() {
r := gin.Default()
r.GET("/redirect", func(c *gin.Context) {
c.Redirect(http.StatusMovedPermanently,"http://www.baidu.com")
})
r.Run(":3000")
}

被折叠的 条评论
为什么被折叠?



