`go run hello.go`找不到“ hello.go”文件

I tried to run the program in command prompt #Go lang- But when i type run "go run hello.go" command i am gettin

CreateFile hello.go:The system cannot find the file specified

Please help to to compile and run the above marked program, Thanks in advance

As you can see from the output of running the dir command earlier up in your shell session, there is indeed no file named "hello.go" in the C:\GOCODE\testproject directory.

When you execute the

go run hello.go

command, the go tool tries to find the file named "hello.go" in the current directory (because the name of that file is relative, so it's being searched in current working directory). There's no such file, and that's what go run tells you.


Unfortunately, from the outlook of your shell session, it appears there are more problems with your setup. And there are problems with your approach to Go.

First, while it looks like you're following this guide (and this is the right thing to do, actually), you misread it.

What it tried to tell you is that you should create the "src" directory (and then the "hello" directory to contain your test project) in the so-called workspace, and a list of workspaces known to Go is stored in the GOPATH environment variable.

As you can see from the go env output, Go thinks you have a single workspace located in C:\Users\Sitaram\Go.

Now it worth reiterating that—contrary to many (if not most) "mainstream" languages,—Go is not "project-based"; instead, it requires all your code to be organized in those workspaces, and it wants to know where these workspaces are. By default—if you did not explicitly set the GOPATH environment variable,—it assumes your single workspace is located in the directory named "go" placed in your "home folder". And that's what you see in the go env output.

Now you have two options:

  • Set the GOPATH env. variable for your user to C:\GOCODE then start another shell—so that it "sees" that variable and allows the go tool to also see it and use). Run go env to verify GOPATH contains C:\GOCODE.

    Then follow the rest of the tutorial document:

    1. Make sure there is the "src" folder directly under the C:\GOCODE.
    2. Create your project folder directory under "src". Let's say, it will be named "hello".
    3. Under "hello", create that "hello.go" file.
    4. Now cd C:\GOCODE\src\hello and then go build — you will have the hello.exe created there.
  • Don't mess with GOPATH and just repeat the steps 2-4 from above in the default workspace—C:\Users\Sitaram\go.

I'd go with the second variant because that inexplicable affection of certain Windows users for polluting the C:\ with random personal data is really an anti-pattern; have your personal belongings in your home folder! Windows has gone a long way getting that right; and almost all Windows software is finally there—understanding that paradigm. So why deviate?

Second, please unlearn go run. I'm not sure the Go developers actually regret implementing it, but people do really misinterpret what this tool is for. It's for one-off throw-away "scripts". Real development is done using go install and, sometimes, go build.

In most cases your normal development routine you use go install exclusively — as it caches the results of compilation of all the packages your project depends on. go build does not do this, and go run does not even preserve the result of the compilation of your project itself. Please read this quick reference card for more info.

After go get gpackage check if there is a yourproject.exe in your bin directory to compile your github package with your project. If not, you have to do cd src/yourproject and type go install and hit enter.