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
文件夾所在的路徑即可。