类似SpringBoot的SpringBoot- Thymelea,gin也提供了类似的功能
package main
import (
"github.com/gin-gonic/gin"
"net/http"
)
func main() {
router := gin.Default()
router.LoadHTMLGlob("html/*")
//router.LoadHTMLFiles("templates/template1.html", "templates/template2.html")
router.GET("/index", func(c *gin.Context) {
c.HTML(http.StatusOK, "index.tmpl", gin.H{
"title": "Main website",
})
})
router.Run(":8080")
}
在main.go目录下新建一个html文件夹 新建一个index.tmpl文件,内容如下:
<html>
<h1>
{{ .title }}
</h1>
</html>
请求:localhost:8080/index:
返回: