普通网友 2016-06-26 19:06
浏览 120
已采纳

如何使模板与gin框架一起使用?

I am newbie to golang. To learn it I have started with a simple web app using gin framework. I have followed the gin doc & configured template file but not able to make it work. I am getting an error -

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

Below is my code -

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")
}

My directory structure is

- gospike
--- templates
------index.tmpl
--- main.go

go install command does not give any error

but on actually running, it gives the above error. I searched & there were similar issues logged on gin's github repo but they are closed now. I have tried various things but I guess I am missing something obvious. What am I missing?

  • 写回答

1条回答 默认 最新

  • douyi9597 2016-06-26 19:39
    关注

    I'm guessing the issue is that you're using a relative filepath to access your templates.

    If I compile and run your code from the gospike directory, it works fine. But if I run gospike from any other directory, I get the same error you were seeing.

    So either you need to always run gospike in the parent directory of templates, or you need to use the absolute path. You could either hard code it:

    router.LoadHTMLGlob("/go/src/github.com/ameykpatil/gospike/templates/*")
    

    or you could do something like

    router.LoadHTMLGlob(filepath.Join(os.Getenv("GOPATH"),
        "src/github.com/ameykpatil/gospike/templates/*"))
    

    but that will fail if you have multiple paths set in your GOPATH. A better long-term solution might be setting a special environment variable like TMPL_DIR, and then just using that:

    router.LoadHTMLGlob(filepath.Join(os.Getenv("TMPL_DIR"), "*"))
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)