简体   繁体   English

Golang初学者,关于Windows:系统找不到指定的文件

[英]Golang beginner, on Windows: System cannot find file specified

Only just now starting with golang, with only a small amount of programming experience before this.刚开始接触golang,之前只有少量的编程经验。 I'm trying to create a script that will summarize certain things from a csv file, but I haven't even gotten past testing out file reading yet.我正在尝试创建一个脚本来总结 csv 文件中的某些内容,但我什至还没有通过文件读取测试。

I was having trouble reading the excel files, and kept getting the "System cannot find file specified" error.我在读取 excel 文件时遇到问题,并不断收到“系统找不到指定文件”错误。 So I thought I'd see if I could at least get it to read a simple text file, using an example from golangbot, which looks like this:所以我想看看我是否至少可以让它读取一个简单的文本文件,使用 golangbot 的示例,如下所示:

package main

import (
    "fmt"
    "io/ioutil"
)

func main() {
    data, err := ioutil.ReadFile("test.txt")
    if err != nil {
        fmt.Println("File reading error", err)
        return
    }
    fmt.Println("Contents of file:", string(data))
}

That simple.就这么简单。 The text file is located within the same folder (in %USERPROFILE%/go/src, and /go/ is my GOPATH) as the actual code file I'm attempting to run, and yes, it is called "test.txt".文本文件位于与我尝试运行的实际代码文件相同的文件夹中(在 %USERPROFILE%/go/src 中,/go/ 是我的 GOPATH),是的,它称为“test.txt” . Yet, every attempt to run gives me the same error message, that the system cannot find the specified file (test.txt).然而,每次尝试运行都会给我相同的错误消息,即系统找不到指定的文件 (test.txt)。

Running any other kind of.go file or building one from this location works just fine.运行任何其他类型的 .go 文件或从该位置构建一个文件都可以正常工作。 I'm seen others with this error, but it seemed like it was always to do with the GOPATH being set wrong.我看到其他人有这个错误,但它似乎总是与 GOPATH 设置错误有关。

I'm frustrated that I even have to ask about something like this, but it's all I could think of right now.我很沮丧,我什至不得不问这样的事情,但这是我现在能想到的。 Is there something wrong with the locations of my files or the GOPATH itself, or is this something different?我的文件位置或 GOPATH 本身有问题,还是有什么不同?

Thank you谢谢

Welp, the problem was solved. 哭了,问题解决了。 Turns out, the actual name of the txt file was test.txt.txt. 事实证明,该txt文件的实际名称为test.txt.txt。 Thanks to notepad and my own lack of awareness. 多亏了记事本和我自己的意识不足。

Bit embarrassing, really. 有点尴尬,真的。 Changing the name worked. 更改名称有效。

when you are trying this with your notepad. 当您用记事本尝试此操作时。 Be cautious while saving it. 保存时要小心。

The file type should be compatible 文件类型应该兼容

  • File -> save as > 文件->另存为>
    FileName : FileName.go FileName:FileName.go
    Save as type : All Files 保存类型:所有文件
  • Now execute as ("folder path">go run FileName.go 现在执行为(“文件夹路径”,然后运行FileName.go

Then the result. 然后结果。 Kudos 荣誉

I have been having a similar issue as well.我也遇到过类似的问题。 I would try to do this in order to fix it.我会尝试这样做以修复它。 delete the file and create a new one.删除文件并创建一个新文件。 That is a simple solution in my opinion.我认为这是一个简单的解决方案。 Make sure to copy the code and then paste onto the new file.确保复制代码,然后粘贴到新文件中。 Mkdir cd into that directory touch or nano create a new file. Mkdir cd 进入该目录 touch 或 nano 创建一个新文件。 then open that file.然后打开那个文件。 then do go run that file name.然后执行 go 运行该文件名。 it should work.它应该工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM