英文:
Go: "stat hello.go: no such file or directory"
问题
刚刚在 Mac OS X Yosemite 10.10.3 上安装了 Go,按照官方网站的入门指南中所述进行操作:
> Mac OS X 包安装程序
>
> 下载包文件,打开它,并按照提示进行安装 Go 工具。该包将 Go 发行版安装到 /usr/local/go
目录下。
>
> 该包应该将 /usr/local/go/bin
目录添加到 PATH 环境变量中。你可能需要重新启动任何已打开的终端会话才能使更改生效。
我现在在 测试安装 部分,其中写着:
> 通过构建一个简单的程序来检查 Go 是否正确安装,具体步骤如下。
>
> 创建一个名为 hello.go
的文件,并将以下程序放入其中:
>
> package main
>
> import "fmt"
>
> func main() {
> fmt.Printf("hello, world\n") }
>
> 然后使用 go 工具运行它:
>
> $ go run hello.go
> hello, world
>
> 如果你看到了"hello, world"的消息,那么你的 Go 安装就正常工作了。
所以,我创建了一个 hello.go
文件,并且由于我无法(也就是不知道如何)访问 /usr/local/go/bin
目录,所以我将它保存在了桌面上。
显然,我得到了以下错误消息:
stat hello.go: no such file or directory
那么,我应该将我的 Go 文件保存在哪里才能运行它们呢?
更新:经过一些研究,我偶然发现了这个视频,解释了如何设置 GOPATH。
如果我想将我的 Go 工作区设置为 user/code/go
,我应该如何编写我的 export GOPATH=
命令?
英文:
Just installed Go on Mac OS X, Yosemite Version 10.10.3, as explained in the Getting Started page of the official website:
> 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.
I am now in the Test your installation section, which states:
> 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.
So, I created a hello.go
file and, since I could not (ie: did not know how to) access the /usr/local/go/bin
directory, I saved it on my desktop.
Obviously, I got the following error message:
stat hello.go: no such file or directory
So, where should I save my Go files to be able to run them?
UPDATE: after some research, I stumbled upond this video, explaining how to set the GOPATH.
If I want my Go workspace to be in user/code/go
, how should I write my export GOPATH=
command?
答案1
得分: 0
如果你正在使用Docker Compose的up/down命令来构建镜像,那么在Dockerfile发生更改后,你需要运行以下命令:
docker compose build
英文:
if your are using docker compose up/down to build image. You need to run
docker compose build
after changes in Dockerfile.
答案2
得分: 0
在我的情况下,只需右键单击文件,然后点击“在集成终端中打开”。然后,要运行你的文件,输入“go run 文件名.go”。
英文:
In my case, it was as simple as right clicking the file, and then clicking 'open in integrated terminal.' Then, to run your file type 'go run filename.go'
答案3
得分: -1
GOPATH
和 PATH
环境变量
GOPATH
环境变量指定了你的工作空间的位置。它默认为你的主目录下的一个名为 go 的目录 ($HOME/go
)。
如果你真的想要将 GOPATH 更改为其他位置,将 GOPATH 添加到你的 shell/bash/zsh
初始化文件 .bash_profile
、.bashrc
或 .zshrc
中。
export GOPATH=/something-else
将 GOPATH/bin
目录添加到你的 PATH
环境变量中,这样你就可以在任何地方运行 Go 程序。
export PATH=$PATH:$(go env GOPATH)/bin
英文:
The GOPATH
and PATH
environment variables
The GOPATH environment variable specifies the location of your workspace. It defaults to a directory named go inside your home directory ($HOME/go
).
If you really want to change your GOPATH to something else add GOPATH to your shell/bash/zsh
initialization file .bash_profile
, .bashrc
or .zshrc
.
export GOPATH=/something-else
Add GOPATH/bin
directory to your PATH
environment variable so you can run Go programs anywhere.
export PATH=$PATH:$(go env GOPATH)/bin
答案4
得分: -2
根据Andrew Gerrand在《编写、构建、安装和测试Go代码》中的解释,我按照以下方式配置了我的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
运行得很顺利。
英文:
Following the explanation of Andrew Gerrand in Writing, building, installing, and testing Go code, I configured my Go workspace as follows:
1. Create workspace
First, since I want my Go code to be in my_user_name/code/go
, I started by creating the corresponding folder:
$ cd code
$ mkdir go
2. Setup GOPATH
Then I configured the GOPATH:
$ cd
$ export GOPATH=$HOME/code/go
$ cd code/go
3. Add files to workspace
Last, I manually (through the Finder) moved my hello.go file into the workspace and ran:
$ go run hello.go
Worked like a charm.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论