douhoujun9304 2013-09-26 17:51
浏览 252
已采纳

转到:为template.ParseFiles指定模板文件名

My current directory structure looks like the following:

App
  - Template
    - foo.go
    - foo.tmpl
  - Model
    - bar.go
  - Another
    - Directory
      - baz.go

The file foo.go uses ParseFiles to read in the template file during init.

import "text/template"

var qTemplate *template.Template

func init() {
  qTemplate = template.Must(template.New("temp").ParseFiles("foo.tmpl"))
}

...

Unit tests for foo.go work as expected. However, I am now trying to run unit tests for bar.go and baz.go which both import foo.go and I get a panic on trying to open foo.tmpl.

/App/Model$ go test    
panic: open foo.tmpl: no such file or directory

/App/Another/Directory$ go test    
panic: open foo.tmpl: no such file or directory

I've tried specifying the template name as a relative directory ("./foo.tmpl"), a full directory ("~/go/src/github.com/App/Template/foo.tmpl"), an App relative directory ("/App/Template/foo.tmpl"), and others but nothing seems to work for both cases. The unit tests fail for either bar.go or baz.go (or both).

Where should my template file be placed and how should I call ParseFiles so that it can always find the template file regardless of which directory I call go test from?

  • 写回答

1条回答 默认 最新

  • doulun7739 2013-12-06 05:55
    关注

    Helpful tip:

    Use os.Getwd() and filepath.Join() to find the absolute path of a relative file path.

    Example

    // File: showPath.go
    package main
    import (
            "fmt"
            "path/filepath"
            "os"
    )
    func main(){
            cwd, _ := os.Getwd()
            fmt.Println( filepath.Join( cwd, "./template/index.gtpl" ) )
    }
    

    First off, I recommend that the template folder only contain templates for presentation and not go files.

    Next, to make life easier, only run files from the root project directory. This will help make the path to an file consistent throughout go files nested within sub directories. Relative file paths start from where the current working directory, which is where the program was called from.

    Example to show the change in current working directory

    user@user:~/go/src/test$ go run showPath.go
    /home/user/go/src/test/template/index.gtpl
    user@user:~/go/src/test$ cd newFolder/
    user@user:~/go/src/test/newFolder$ go run ../showPath.go 
    /home/user/go/src/test/newFolder/template/index.gtpl
    

    As for test files, you can run individual test files by supplying the file name.

    go test foo/foo_test.go
    

    Lastly, use a base path and the path/filepath package to form file paths.

    Example:

    var (
      basePath = "./public"
      templatePath = filepath.Join(basePath, "template")
      indexFile = filepath.Join(templatePath, "index.gtpl")
    ) 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器