2016-06-26 4 views
1

나는 골란에 초보자입니다. 나는 그것을 배우기 위해서 gin 프레임 워크를 사용하는 간단한 웹 앱으로 시작했다. 나는 진 진보적 인 문서 & 템플릿 파일을 따라 갔지만 제대로 작동하지는 못했습니다. 나는이 오류가 무엇입니까 - 아래진 프레임 워크에서 템플릿을 작동시키는 방법은 무엇입니까?

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에 기록 된 비슷한 문제가 있었지만 지금은 닫았습니다. 여러 가지 시도를했지만 분명히 뭔가 빠져있는 것 같습니다. 내가 뭘 놓치고 있니?

답변

3

필자는 템플릿에 액세스하기 위해 상대 파일 경로를 사용하고있는 것으로 추측하고 있습니다.

코드 gospike 디렉토리에서 컴파일하고 실행하면 정상적으로 작동합니다. 그러나 다른 디렉토리에서 gospike을 실행하면보고있는 것과 같은 오류가 발생합니다.

그러므로 templates의 상위 디렉토리에서 항상 gospike을 실행해야하거나 절대 경로를 사용해야합니다. 당신은 할 수 중 하나를 하드 코드 그것을 :

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

또는 당신이 할 수있는 일

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

하지만 당신은 당신의 GOPATH에 설정된 여러 경로가있을 경우 그 실패합니다

있다. 더 나은 장기적인 해결책은 TMPL_DIR 같은 특별한 환경 변수를 설정 할 수 있으며 다음 단지를 사용 :

router.LoadHTMLGlob(filepath.Join(os.Getenv("TMPL_DIR"), "*")) 
+0

완벽하게 일했다! – ameykpatil

관련 문제