可以使用gin的Group方法来设置全局路由后缀。具体步骤如下:

  1. 在main函数中创建一个gin引擎对象。
r := gin.Default()
  1. 调用Group方法创建一个路由组,并使用Set方法设置路由后缀。
r.Group("").SetRouterSuffix(".html")
  1. 在路由组中添加路由处理函数。
r.GET("/index", func(c *gin.Context) {
    c.HTML(http.StatusOK, "index", nil)
})

这样,在访问/index路由时,会自动在后面添加.html后缀,即访问/index.html。

完整代码示例:

package main

import (
	"net/http"

	"github.com/gin-gonic/gin"
)

func main() {
	r := gin.Default()

	r.Group("").SetRouterSuffix(".html")

	r.GET("/index", func(c *gin.Context) {
		c.HTML(http.StatusOK, "index", nil)
	})

	r.Run(":8080")
}
``
gin怎么统一设置html路由后缀

原文地址: https://www.cveoy.top/t/topic/ex99 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录