我是golang的新手。为了学习它,我从一个使用gin框架的简单 Web 应用程序开始。我遵循了 gin doc 和配置的模板文件,但无法使其工作。我收到一个错误 -
panic: html/template: pattern matches no files: `templates/*` goroutine 1 [running]: html/template.Must /usr/local/Cellar/go/1.5.2/libexec/src/html/template/template.go:330 github.com/gin-gonic/gin.(*Engine).LoadHTMLGlob /Users/ameypatil/deployment/go/src/github.com/gin-gonic/gin/gin.go:126 main.main() /Users/ameypatil/deployment/go/src/github.com/ameykpatil/gospike/main.go:17
下面是我的代码 -
package main
import (
"net/http"
"github.com/gin-gonic/gin"
)
func main() {
//os.Setenv("GIN_MODE", "release")
//gin.SetMode(gin.ReleaseMode)
// Creates a gin router with default middleware:
// logger and recovery (crash-free) middleware
router := gin.Default()
router.LoadHTMLGlob("templates/*")
//router.LoadHTMLFiles("templates/index.tmpl")
router.GET("/", func(c *gin.Context) {
c.HTML(http.StatusOK, "index.tmpl", gin.H{
"title": "GoSpike",
})
})
// By default it serves on :8080 unless a
// PORT environment variable was defined.
router.Run(":4848")
}
我的目录结构是
- gospike
--- templates
------index.tmpl
--- main.go
go install
命令没有给出任何错误
但在实际运行时,它给出了上述错误。我搜索并在 gin 的 github repo 上记录了类似的问题,但它们现在已关闭。我尝试了各种各样的东西,但我想我错过了一些明显的东西。我错过了什么?