Taskurotta
Taskurotta

Reputation: 159

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. 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

Upvotes: 7

Views: 24133

Answers (3)

isaac marco
isaac marco

Reputation: 1

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. then open that file. then do go run that file name. it should work.

Upvotes: 0

CdVr
CdVr

Reputation: 313

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
    Save as type : All Files
  • Now execute as ("folder path">go run FileName.go

Then the result. Kudos

Upvotes: 0

Taskurotta
Taskurotta

Reputation: 159

Welp, the problem was solved. Turns out, the actual name of the txt file was test.txt.txt. Thanks to notepad and my own lack of awareness.

Bit embarrassing, really. Changing the name worked.

Upvotes: 6

Related Questions