去:"stat hello.go: no such file or directory"

Go: "stat hello.go: no such file or directory"

刚装上MacOSX,Yosemite版本10.10.3,官网Getting Started页面说明:

Mac OS X package installer

Download the package file, open it, and follow the prompts to install the Go tools. The package installs the Go distribution to /usr/local/go.

The package should put the /usr/local/go/bin directory in your PATH environment variable. You may need to restart any open Terminal sessions for the change to take effect.

我现在在测试您的安装部分,其中指出:

Check that Go is installed correctly by building a simple program, as follows.

Create a file named hello.go and put the following program in it:

package main

import "fmt"

func main() {
    fmt.Printf("hello, world\n") }

Then run it with the go tool:

$ go run hello.go
hello, world

If you see the "hello, world" message then your Go installation is working.

因此,我创建了一个 hello.go 文件,由于我无法(即:不知道如何)访问 /usr/local/go/bin 目录,我将其保存在我的桌面上。

显然,我收到以下错误消息:

stat hello.go: no such file or directory

那么,我应该在哪里保存我的 Go 文件以便能够 运行 它们?

更新:经过一些研究,我偶然发现了这个视频,解释了如何设置 GOPATH。

如果我希望我的 Go 工作空间在 user/code/go 中,我应该如何编写我的 export GOPATH= 命令?

根据 Writing, building, installing, and testing Go code 中 Andrew Ger运行d 的解释,我将我的 Go 工作区配置如下:

1.创建工作区

首先,因为我希望我的 Go 代码在 my_user_name/code/go 中,所以我首先创建相应的文件夹:

$ cd code
$ mkdir go

2。设置 GOPATH

然后我配置了GOPATH:

$ cd
$ export GOPATH=$HOME/code/go
$ cd code/go

3。将文件添加到工作区

最后,我手动(通过 Finder)将我的 hello.go 文件移动到工作区并 运行:

$ go run hello.go

工作得很好。