Golang无法将文件写入目录中。

huangapple go评论104阅读模式
英文:

Golang cannot write file into directory

问题

情况:

我正在尝试将文件写入一个目录,如下所示:

func (p *Page) Save() error {
    filepath := DerivePath(p.Title)
    fmt.Println(filepath)
    content, _ := json.MarshalIndent(p, "", "    ")
    err := ioutil.WriteFile(filepath, content, 0600)
    return err
}

问题:

在第5行出现以下错误:

打开 data/Testpage.json: 系统找不到指定的路径。

  • 我已经尝试在写入之前使用 os.Create 创建文件,但也不起作用。
  • data 目录加载是正常的。只有将文件写入目录时才会失败。

附加信息:

我的项目结构如下:

│   .gitignore
│   .project
│
├───bin
│       main.exe
│
├───data
│       Welcome.json
│
├───pkg
│   └───windows_amd64
│           page.a
│
├───src
│   ├───main
│   │       main.go
│   │
│   └───page
│           page.go
│           page_test.go
│
└───templates
        view.html

如上所述,读取 data/Welcome.json 是正常的(使用 io/ioutils.ReadFile)。
源代码可在 https://gitlab.com/thyaris/Wiki 上找到。

执行 D:\GitWorkspaces\Wiki\wiki>go test -v page 会输出以下内容:

=== RUN   TestSave
data/Testpage.json
--- FAIL: TestSave (0.00s)
        page_test.go:15: 打开 data/Testpage.json: 系统找不到指定的路径。
        page_test.go:19: 'Testpage.json' 未被创建
=== RUN   TestLoadPage
--- FAIL: TestLoadPage (0.00s)
        page_test.go:26: 加载时出错
        page_test.go:32: 文件内容不匹配
=== RUN   TestDelete
--- PASS: TestDelete (0.00s)
FAIL
exit status 1
FAIL    page    0.094s
英文:

Situation:

I'm trying to write a file into a directory, like shown as follows:

func (p *Page) Save() error {
    filepath := DerivePath(p.Title)
    fmt.Println(filepath)
    content, _ := json.MarshalIndent(p, "", "    ")
    err := ioutil.WriteFile(filepath, content, 0600)
    return err
}

Problem:

The following error occurs in line 5:

> open data/Testpage.json: The system cannot find the path specified.

  • I already tried to create the file before writing with os.Create, but it doesn't work either.
  • Loading from the data directory works just fine. Only writing new files into the directory fails.

Additional information:

My project structure is as follows:

│   .gitignore
│   .project
│
├───bin
│       main.exe
│
├───data
│       Welcome.json
│
├───pkg
│   └───windows_amd64
│           page.a
│
├───src
│   ├───main
│   │       main.go
│   │
│   └───page
│           page.go
│           page_test.go
│
└───templates
        view.html

As mentioned above, reading data/Welcome.json works just fine (by using io/ioutils.ReadFile).
The source is available on https://gitlab.com/thyaris/Wiki.

Executing D:\GitWorkspaces\Wiki\wiki>go test -v page writes the following output:

=== RUN   TestSave
data/Testpage.json
--- FAIL: TestSave (0.00s)
        page_test.go:15: open data/Testpage.json: The system cannot find the path specified.
        page_test.go:19: 'Testpage.json' was not created
=== RUN   TestLoadPage
--- FAIL: TestLoadPage (0.00s)
        page_test.go:26: Error while loading
        page_test.go:32: File content did not match
=== RUN   TestDelete
--- PASS: TestDelete (0.00s)
FAIL
exit status 1
FAIL    page    0.094s

答案1

得分: 2

你的问题在于测试引擎没有按照你期望的工作目录来运行可执行文件。它并没有使用由你的shell或IDE定义的工作目录,而是将其设置为被测试代码的源目录。(我也曾经遇到过这个问题,很久以前的事情了 Golang无法将文件写入目录中。 我几乎忘记了这个问题...)

简单的解决方案是修改DerivePath,使其可以从外部设置前缀,然后在测试开始时将其更改为你需要的路径。当然,还有其他(可能更好的)解决方案。

英文:

Your problem here is that the test engine is not running your executable with the working directory you expect. Instead of using the working directory defined by your shell or IDE, it is setting it to the source directory of the code being tested. (I had this bite me too once, long ago Golang无法将文件写入目录中。 I had almost forgotten about that...)

The simple solution is to change DerivePath so that you can set the prefix externally, then change it to a the path you need at the beginning of your tests. There are other (possibly better?) solutions of course.

huangapple
  • 本文由 发表于 2017年7月26日 06:36:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/45314609.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定