在我的 Go 项目中,我使用一个函数来打开特定文件并返回其内容。该文件存储在另一个目录中,但仍在我的项目目录中。
package infrastructure
func openKey() ([]byte, error) {
if path, err := filepath.Abs("../security/key.rsa"); err != nil {
return nil, err
}
return ioutil.ReadFile(path)
}
如果我从单元测试中调用它,则此函数有效。但是,如果我在程序中调用相同的函数,则会出现以下错误:
2015/08/13 15:47:54 打开/me/go/src/github.com/myaccount/security/key.rsa:没有这样的文件或目录
文件的正确绝对路径是:
/me/go/src/github.com/myaccount/ myrepo /security/key.rsa
使用该openKey函数的两个代码(来自我的程序和单元测试)都在同一个包中:infrastructure
这是我执行程序的方式:
去安装 && ../../../../bin/myproject
以及我如何执行我的单元测试:
去测试./...
最后是我项目的目录结构:
go/src/github.com/myaccount/myrepo/:
- main.go
- security:
- key.rsa // The file that I want to open
- ...
- infrastructure
- openFile.go // The file with the func `openKey``
- server.go // The file with the func that call the func `openKey`
- openFile_test.go // The unit test that calls the func `openKey`
编辑:
以下是我的程序二进制文件所在位置的绝对路径:
/Users/me/Documents/Développement/Jean/go/bin
以及我的单元测试所在的位置:
/var/folders/tj/8ywtc7pj3rs_j0y6zzldwh5h0000gn/T/go-build221890578/github.com/myaccount/myrepo/infrastructure/_test
有什么建议吗?
慕哥6287543
相关分类