【问题标题】:path.join go up two directoriespath.join 上两个目录
【发布时间】:2020-07-26 17:56:04
【问题描述】:

我的 main.go 文件位于路径 project/cmd/project/main.go 中,我的项目配置存储在 project/config 中。我正在尝试从 main.go 文件访问我的配置文件,但我在创建配置文件路径时遇到问题。我试过path.join("..", "..", projectDir, "config") 但没用。

【问题讨论】:

  • 看起来应该是../../config
  • 没有以什么方式工作?您看到了什么错误或其他意外输出?

标签: go path config


【解决方案1】:

应该是path.join(projectDir, "..", "..", "config")。首先是基本路径。

这是我的测试代码:

func TestPath(t *testing.T) {
    path, _ := os.Getwd()
    t.Log(path)
    newPath := path2.Join(path, "..", "..", "config")
    t.Log(newPath)
}

还有输出:

$ go test ./... -run TestPath -v -count=1                                                                                        01:04:45
=== RUN   TestPath
    TestPath: main_test.go:12: /home/fahim/Projects/Golang/go-validator
    TestPath: main_test.go:14: /home/fahim/Projects/config
--- PASS: TestPath (0.00s)
PASS
ok      go-validator    0.006s

【讨论】:

  • 非常感谢,这解决了我的问题...我怎么会这么笨...
  • 每个人都会犯错,不用担心
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-18
  • 2023-04-05
相关资源
最近更新 更多