英文:
Go not running hello world example
问题
我正在Ubuntu 14.04上安装Go,并且相信我已经正确设置了GOPATH,但似乎无法运行go install hello.go文件。我首先解决了遇到的任何路径错误,但仍然没有看到成功运行的结果。
Go已安装在/etc/go目录下。
$ go env
GOARCH="amd64"
GOBIN=""
GOCHAR="6"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/etc/go/packages"
GORACE=""
GOROOT="/etc/go"
GOTOOLDIR="/etc/go/pkg/tool/linux_amd64"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0"
CXX="g++"
CGO_ENABLED="1"
我创建了文件,并通过nano编辑复制了hello world示例,但运行时没有任何反应...
$ mkdir -p $GOPATH/src/github.com/hackg
$ nano $GOPATH/src/github.com/hackg/hello/hello.go
$ go install github.com/hackg/hello
$
问题 - 它应该在那里显示"hello world",证明Go正在正确构建文件,但我得到的只是一个新的终端提示符,准备输入新的命令,没有"HELLO WORLD"。
我尝试查看其他stackoverflow帖子,但没有找到解决方法-例如https://stackoverflow.com/questions/19073794/go-golang-does-not-make-sense-that-i-have-to-have-files-before-import
英文:
I am installing go in Ubuntu 14.04 and believe I have my GOPATH$ set right and can't seem to run this go install hello.go file. I got rid of any path errors that I encountered first, but I still am not seeing a successful run
Go is installed in /etc/go
$ go env
GOARCH="amd64"
GOBIN=""
GOCHAR="6"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/etc/go/packages"
GORACE=""
GOROOT="/etc/go"
GOTOOLDIR="/etc/go/pkg/tool/linux_amd64"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0"
CXX="g++"
CGO_ENABLED="1"
And here I made the file, edited via nano to copy the hello world example, but when I run it, nothing happens...
$ mkdir -p $GOPATH/src/github.com/hackg
$ nano $GOPATH/src/github.com/hackg/hello/hello.go
$ go install github.com/hackg/hello
$
QUESTION - it is supposed to display hello world there, proving Go is building files properly, but all I get is a fresh terminal prompt ready for a new command, no HELLO WORLD
I tried looking at other stackoverflow posts with no luck - ex https://stackoverflow.com/questions/19073794/go-golang-does-not-make-sense-that-i-have-to-have-files-before-import
答案1
得分: 4
不,那个命令不会运行你的程序;go install
只是生成了一个二进制文件,你可以用$GOPATH/bin/hello
来运行它。你可以在~/.bashrc
或者等效的文件中添加一行export PATH=$GOPATH/bin:$PATH
,然后打开一个新的终端(比如关闭并重新打开终端程序),这样你就可以直接用hello
来调用它了。使用go run
来运行文件会进行编译和运行,但是对于"真正"的程序而言,最好从go install
开始,因为这是你将要用于"真正"的程序而不是快速测试的方式。
(听起来你在设置GOPATH等方面走在正确的轨道上,但是对于设置Go环境的一般方法,你可能会发现这个问题有帮助。)
英文:
No, that command won't run your program; go install
just made a binary you can run with $GOPATH/bin/hello
. You can add to your ~/.bashrc
or equivalent the line export PATH=$GOPATH/bin:$PATH
, and open a new shell (like by closing and reopening your terminal program), so you can call it with just hello
. go run
ning the file would compile and run, but it's reasonable to start with go install
because that's what you're going to use for "real" programs as opposed to quick tests.
(Sounds like you're on the right track with a GOPATH and all, but for setting up a Go environment generally you might find this question helpful.)
答案2
得分: 1
我做了以下操作,对我来说运行得很好。你可以尝试一下:
在终端中运行以下命令:
sudo apt-get install golang
这将在你的系统中安装golang。
在你的.bashrc文件中添加以下行:
export GOPATH=$HOME/go
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
现在,关闭并重新打开你的shell。然后运行以下命令来检查你系统中安装的go版本:
go version
假设hello.go是包含hello world程序的文件,然后执行以下操作:
go run hello.go
希望对你有帮助。
英文:
I did the following and it works fine for me. You may try it:-
Run the following command in your terminal:-
sudo apt-get install golang
This will install golang in your system
Add the following lines in your .bashrc file:-
export GOPATH=$HOME/go
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
Now, close and open your shell. And run the following command to check the version of go installed in your system:-
go version
Suppose hello.go is your file containing hello world program, then do the following:-
go run hello.go
Hope it helps.
答案3
得分: 0
我明白了,我以为它会运行命令,而不仅仅是安装它。
安装完成后,我只需要输入 hello 并按回车键。
$ hello
hello, world
我想我没有仔细阅读说明。谢谢 twotwotwo。
英文:
I figured it out, I thought it would run the command, not just install it.
all I had to do once installed was type hello and hit enter
$ hello
hello, world
I suppose I didn't read the directions well. thanks twotwotwo
答案4
得分: 0
这里是获取你的hello world示例工作的指令。
编译和安装包及其依赖项:
go install github.com/user/hello
它会将一个名为hello(或hello.exe)的可执行命令放置在工作区的bin目录中。
将Go程序构建为二进制文件:
go build github.com/user/hello
运行:
$ $GOPATH/bin/hello
hello, world
英文:
This gives instructions to get your hello world example working.
To compile and install packages and dependencies:
go install github.com/user/hello
It will put an executable command named hello (or hello.exe) inside the bin directory of your workspace.
To build go program into binaries:
go build github.com/user/hello
To run:
$ $GOPATH/bin/hello
hello, world
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论