猿问

path.join 上两个目录

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



www说
浏览 134回答 1
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)PASSok      go-validator    0.006s
随时随地看视频慕课网APP

相关分类

Go
我要回答