doude4201
2018-08-02 12:57
浏览 731
已采纳

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

Only just now starting with golang, with only a small amount of programming experience before this. 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.

I was having trouble reading the excel files, and kept getting the "System cannot find file specified" error. 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:

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". Yet, every attempt to run gives me the same error message, that the system cannot find the specified file (test.txt).

Running any other kind of .go file or building one from this location works just fine. I'm seen others with this error, but it seemed like it was always to do with the GOPATH being set wrong.

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?

Thank you

图片转代码服务由CSDN问答提供 功能建议

仅从golang开始,在此之前只有很少的编程经验。 我正在尝试创建一个脚本,该脚本将汇总csv文件中的某些内容,但是我还没有过去测试文件读取的方法。

我在读取excel时遇到了麻烦 文件,并不断出现“系统找不到指定的文件”错误。 因此,我想我会用golangbot的示例来看看是否至少可以读取一个简单的文本文件,如下所示:

  package main 
  
import(
“ fmt” 
“ io / ioutil” 
)
 
func main(){
 data,err:= ioutil.ReadFile(“ test.txt”)
 if err!= nil  {
 fmt.Println(“文件读取错误”,错误)
返回
} 
 fmt.Println(“文件内容:”,字符串(数据))
} 
   
 
 

就这么简单。 该文本文件与我尝试运行的实际代码文件位于同一文件夹(位于%USERPROFILE%/ go / src中,/ go /是我的GOPATH)中,是的,它称为“ test.txt” 。 但是,每次尝试运行都会给我同样的错误消息,即系统找不到指定的文件(test.txt)。

运行任何其他类型的.go文件或从中构建文件 这个位置工作正常。 我见过其他人遇到此错误,但似乎总是将GOPATH设置为错误。

我很沮丧,我什至不得不询问类似这样的问题 ,但这就是我现在能想到的全部。 我的文件或GOPATH本身的位置是否有问题,还是有些不同?

谢谢

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

相关推荐 更多相似问题