在nodejs中,我使用__dirname.在Golang中相当于什么?
我用Google搜索并发现了这篇文章http://andrewbrookins.com/tech/golang-get-directory-of-the-current-file/.他使用以下代码的地方
_, filename, _, _ := runtime.Caller(1)
f, err := os.Open(path.Join(path.Dir(filename), "data.csv"))
Run Code Online (Sandbox Code Playgroud)
但这是在Golang中正确的方式或惯用方式吗?
如何GOPATH从代码块中获取当前值?
runtime只有GOROOT:
// GOROOT returns the root of the Go tree.
// It uses the GOROOT environment variable, if set,
// or else the root used during the Go build.
func GOROOT() string {
s := gogetenv("GOROOT")
if s != "" {
return s
}
return defaultGoroot
}
Run Code Online (Sandbox Code Playgroud)
我可以创建一个已经GOROOT替换的函数GOPATH,但是有一个构建吗?
我需要引用patients.json从patients.go,这里的文件夹结构:

如果我做:
filepath.Abs("../../conf/patients.json")
它适用go test ./...但却失败了revel run
如果我做:
filepath.Abs("conf/patients.json")
恰恰相反(狂欢很好,但测试失败).
有没有办法正确引用该文件,以便它适用于测试和正常程序运行?