之前用gin加载HTML模板都没问题,这次换了一个项目文件夹,一直报错

您所在的位置:网站首页 html项目管理器没了 之前用gin加载HTML模板都没问题,这次换了一个项目文件夹,一直报错

之前用gin加载HTML模板都没问题,这次换了一个项目文件夹,一直报错

2023-03-10 05:13| 来源: 网络整理| 查看: 265

package router import ( "demo02/goproject/controller" "github.com/gin-gonic/gin" ) func Start() { r := gin.Default() r.LoadHTMLGlob("templates/*") r.GET("/", controller.Strings) r.GET("/json", controller.TestJSON) r.GET("/index", controller.Index) r.Run(":8001") }

——————————————————————————————————————————————————————————————————————

package controller import ( "github.com/gin-gonic/gin" "net/http" ) type User struct { Title string Content string } func Strings(c *gin.Context) { c.String(200, "Hello,Gin!") } func TestJSON(c *gin.Context) { c.JSON(200, gin.H{ "name": "jack", "age": 18, }) } func Index(c *gin.Context) { news := &User{ Title: "新闻标题", Content: "新闻详情", } c.HTML(http.StatusOK, "index.html", gin.H{ "title": "新闻页面", "news": news, }) } —————————————————————————————————————————————————————————————————— package main import "demo02/goproject/router" func main() { router.Start() }

这是报错:

PS D:\GoProject\demo02> go run .\main.go [GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached. [GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production. - using env: export GIN_MODE=release - using code: gin.SetMode(gin.ReleaseMode) panic: template: index.html:14: bad character U+007D '}' goroutine 1 [running]: html/template.Must(...) C:/Program Files/Go/src/html/template/template.go:368 github.com/gin-gonic/gin.(*Engine).LoadHTMLGlob(0xc0003e29c0, {0x1303c86, 0xb}) C:/Users/wen/go/pkg/mod/github.com/gin-gonic/[email protected]/gin.go:255 +0x306 demo02/goproject/router.Start() D:/GoProject/demo02/router/router.go:10 +0x33 main.main() D:/GoProject/demo02/main.go:6 +0x17 exit status 2 PS D:\GoProject\demo02>


【本文地址】


今日新闻


推荐新闻


    CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3