goland中使用gin渲染html文件时出现 pattern matches no files: 错误
goland中使用gin渲染html文件时出现 pattern matches no files: 错误问题表现出现了如下错误:panic: html/template: pattern matches no files: `templates/**/*`
·
goland中使用gin渲染html文件时出现 pattern matches no files: 错误
问题表现
使用golang进行web开发时,渲染模板文件却找不到模板文件。代码如下:
r := gin.Default()
r.LoadHTMLGlob("templates/**/*")
r.GET("posts/index", func(c *gin.Context) {
c.HTML(http.StatusOK, "posts/index.tmpl", gin.H{
"title": "Posts",
})
})
r.Run(":8080")
提示如下错误:
panic: html/template: pattern matches no files: `templates/**/*`
问题分析
使用goland时,默认的工作路径为项目根目录,如果代码和模板文件都位于src目录下,则会找不到。
解决方案
在goland中修改当前工作路径,方法如下:
1、运行配置中,点击Edit Configurations.
2、修改工作路径
修改工作路径为templates文件夹所在的路径即可。
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐

所有评论(0)