gin 頁面重定向


兩種gin頁面重定向方式

redirect:

package main
 
import (
    "github.com/gin-gonic/gin"
    "net/http"
)
 
 
func main() {
 
    r := gin.Default()
 
    r.GET("/redirect", func(c *gin.Context) {
        c.Redirect(http.StatusMovedPermanently,"http://www.baidu.com")
    })
 
    r.Run(":8080")
}

html rendering:

func main() {
    router := gin.Default()
    router.LoadHTMLGlob("templates/*")

    router.GET("/index", func(c *gin.Context) {
        c.HTML(http.StatusOK, "index.tmpl", gin.H{
            "title": "Main website",
        })
    })
    router.Run(":8080")
}

其中 

router.LoadHTMLGlob("templates/*")是設置前端頁面目錄


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM