相关疑难解决方法(0)

如何获取当前正在运行的文件的目录?

在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中正确的方式或惯用方式吗?

go

213
推荐指数
11
解决办法
16万
查看次数

如何从代码中获取当前的GOPATH

如何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,但是有一个构建吗?

go

17
推荐指数
2
解决办法
1万
查看次数

如何从代码和测试中引用相关文件

我需要引用patients.jsonpatients.go,这里的文件夹结构:

在此输入图像描述

如果我做:

filepath.Abs("../../conf/patients.json")

它适用go test ./...但却失败了revel run

如果我做:

filepath.Abs("conf/patients.json")

恰恰相反(狂欢很好,但测试失败).

有没有办法正确引用该文件,以便它适用于测试和正常程序运行?

relative-path go revel

11
推荐指数
1
解决办法
3048
查看次数

标签 统计

go ×3

relative-path ×1

revel ×1