英文:
Go setup on ubuntu
问题
我正在尝试在Ubuntu上设置Go开发环境,但没有成功。按照这里的指示进行操作:https://github.com/golang/go/wiki/Ubuntu
首先运行以下命令安装Go:
sudo apt-get install golang
然后创建一个目录并设置GOPATH:
mkdir $HOME/golang
export GOPATH=$HOME/golang
但是没有成功。即使像go version
这样简单的命令也会报错,显示以下错误信息:
go: cannot find GOROOT directory: /usr/local/opt/go/libexec
我在网上搜索的每个地方都说不要设置GOROOT
。请帮忙,我不知道接下来该怎么做。这是在全新的虚拟机上进行的全新安装。
以下是一些环境变量的输出信息:
GOARCH="amd64"
GOBIN=""
GOCHAR="6"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/isaac/golang"
GORACE=""
GOROOT="/usr/local/opt/go/libexec"
GOTOOLDIR="/usr/local/opt/go/libexec/pkg/tool/linux_amd64"
TERM="dumb"
CC="gcc"
GOGCCFLAGS="-g -O2 -fPIC -m64 -pthread"
CXX="g++"
CGO_ENABLED="1"
英文:
I am trying to setup a go dev environment on Ubuntu, and having no luck. Following directions here https://github.com/golang/go/wiki/Ubuntu
sudo apt-get install golang
Then I
mkdir $HOME/golang
export GOPATH=$HOME/golang
No dice. Even doing something simple like go version
throws the following error:
go: cannot find GOROOT directory: /usr/local/opt/go/libexec
Everywhere I look online says simply not to set GOROOT
. Please help, I don't understand where to go from here. This is a fresh install on a fresh VM.
GOARCH="amd64"
GOBIN=""
GOCHAR="6"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/isaac/golang"
GORACE=""
GOROOT="/usr/local/opt/go/libexec"
GOTOOLDIR="/usr/local/opt/go/libexec/pkg/tool/linux_amd64"
TERM="dumb"
CC="gcc"
GOGCCFLAGS="-g -O2 -fPIC -m64 -pthread"
CXX="g++"
CGO_ENABLED="1"
答案1
得分: 10
在我的Ubuntu机器上,我按照以下简单的步骤安装了Go语言:
$ wget https://storage.googleapis.com/golang/go1.4.2.linux-amd64.tar.gz
$ sudo tar -C /usr/local -xzf go1.4.2.linux-amd64.tar.gz
$ rm go1.4.2.linux-amd64.tar.gz
将Go添加到你的$PATH变量中:
$ mkdir $HOME/go
$ nano ~/.bashrc
export GOPATH=$HOME/go
export PATH=$PATH:/usr/local/go/bin:$GOPATH/bin
$ source ~/.bashrc
这样就可以正常工作了。
英文:
On my ubuntu machine i installed go by following those simple steps:
$ wget https://storage.googleapis.com/golang/go1.4.2.linux-amd64.tar.gz
$ sudo tar -C /usr/local -xzf go1.4.2.linux-amd64.tar.gz
$ rm go1.4.2.linux-amd64.tar.gz
Add go to your $PATH variable
$ mkdir $HOME/go
$ nano ~/.bashrc
export GOPATH=$HOME/go
export PATH=$PATH:/usr/local/go/bin:$GOPATH/bin
$ source ~/.bashrc
This Works just fine.
答案2
得分: 8
所以我最终解决了这个问题,我真是太蠢了。我有一个脚本影响了$GOROOT
,并且学到了很多。以下是我得出的重要教训:
-
不要使用
sudo apt-get install golang
,它已经过时了,这样做意味着你现在必须还原安装。sudo apt-get install golang-go
也已经过时了。不要使用apt-get
。 -
sudo apt-get purge golang
不会重置环境变量或删除所有与go相关的文件夹。 -
在尝试重新安装之前,重要的清理工作是
unset GOPATH GOHOME GOROOT
。 -
只能通过从golang网站下载的
tar.gz
文件进行安装。 -
GOROOT表示go的内部文件所在的文件夹,基本上就是
tar.gz
文件的内容在你的系统上的位置。通常是/usr/local/go
。 -
GOHOME不需要设置。如果设置了,使用
unset
命令取消设置。 -
GOPATH是你的工作空间的位置,你需要使用
mkdir
命令创建该文件夹,并设置GOPATH环境变量。 -
你的路径需要包含
$GOROOT/bin:$GOPATH/bin
,才能使设置生效。
使用影响.zshrc
、bashrc
或profile
的自定义脚本时,不应该包含设置$GOROOT
的内容!
英文:
So I eventually figured this out, and boy was it dumb on my part. I had a script that was effecting $GOROOT
, and learned alot. Here are the big lessons:
-
Do NOT use
sudo apt-get install golang
it is out of date and doing so means you now have to revert the install.sudo apt-get install golang-go
is also out of date. Just don't useapt-get
. -
sudo apt-get purge golang
does not reset environment variables or delete all go related folders. -
unset GOPATH GOHOME GOROOT
is important cleanup before trying another install -
ONLY install via
tar.gz
from the golang website -
GOROOT means the folder where go's internal files live, so basically where ever the contents of the
tar.gz
lives on your system. Typically/usr/local/go
-
GOHOME does not need to be set. If you set it, use
unset
-
GOPATH is the location of your workspace, you need to
mkdir
to create that folder as well as set the GOPATH environment variable. -
Your path needs to included
$GOROOT/bin:$GOPATH/bin
for the setup to function.
Usage of custom scripts that effect .zshrc
or bashrc
or profile
should not contain setting of $GOROOT
!!
答案3
得分: 1
对于来自Ubuntu软件仓库的golang-go包
这个回答是针对默认的Ubuntu软件仓库中的golang-go包,而不是使用ubuntu-make安装的go-lang包。
理解你的问题的关键在于以下这行代码:
GOROOT="/usr/local/opt/go/libexec"
如果你输入命令 echo $GOROOT
,它会返回:
/usr/local/opt/go/libexec
这不是你在默认的Ubuntu软件仓库中安装的golang-go中想要的GOROOT。在Ubuntu中,默认的golang-go安装中,GOROOT指的是go安装的根目录。运行命令 which go
返回 /usr/bin/go
,检查 /usr/bin/go
文件会发现它是一个符号链接,指向位于 /usr/lib/go-1.6/bin/go
的go可执行文件。这就是你缺少的GOROOT。如果你使用命令 sudo apt install golang-go
安装了golang-go,GOROOT就是由golang-go安装的所有go文件的根目录,即 /usr/lib/go-1.6
!
接下来需要将GOROOT的更改应用到 /etc/environment
文件中,可以使用以下命令在nano文本编辑器中编辑环境文件:
sudo nano /etc/environment
在 /etc/environment
中的 PATH="
后面插入以下文本以将其添加到PATH中:
/usr/lib/go-1.6:
按下键盘组合键 Ctrl+O,然后按下 Enter 保存正在编辑的文件到当前位置。按下键盘组合键 Ctrl+X 退出nano。
/usr/lib/go-1.6
后面的 :
字符是分隔符,用于分隔go路径和下一个路径。建议使用以下命令检查你的工作是否准确:
cat /etc/environment
现在你已经更新了PATH,需要通过注销并重新登录来重新加载 /etc/environment
。
英文:
For golang-go from the Ubuntu repositories
This answer refers to the golang-go package from the default Ubuntu repositories, not the go-lang package that can be installed using ubuntu-make.
The key to understanding your question is the following line:
GOROOT="/usr/local/opt/go/libexec"
If you typed the command echo $GOROOT
it would return:
/usr/local/opt/go/libexec
This is not what you want GOROOT to be in an installation of golang-go from the default Ubuntu repositories. In a default golang-go installation in Ubuntu GOROOT refers to the root of the directories where go is installed. Running the command which go
returns /usr/bin/go
and examining the /usr/bin/go
file reveals that it is symlinked to the go executable file located at /usr/lib/go-1.6/bin/go
. There is your missing GOROOT. If you installed golang-go using the command sudo apt install golang-go
GOROOT is the root directory of all the go files that are installed by golang-go; it is /usr/lib/go-1.6
!
It remains to apply the change in GOROOT to /etc/environment
by editing the environment file in nano text editor with the following command:
sudo nano /etc/environment
Right after where it says PATH="
in /etc/environment
insert the following text to add it to the PATH:
/usr/lib/go-1.6:
Press the keyboard combination <kbd>Ctrl</kbd>+<kbd>O</kbd> and after that press <kbd>Enter</kbd> to save the file being edited in nano to its current location. Press the keyboard combination <kbd>Ctrl</kbd>+<kbd>X</kbd> to exit nano.
The :
character after /usr/lib/go-1.6
is the delimiter character which separates the go path from the next path. It is advisable to check your work for accuracy with the following command:
cat /etc/environment
Now that you have updated the PATH you need to reload /etc/environment
by logging out and logging back in.
答案4
得分: 0
添加到 @sadlil 的答案中,你可以有一个复合的 GOPATH
。你可能有一个单独的目录,其中包含你所有的项目,并且你想保持这个目录结构。在你的 all_projects
目录中创建一个 go 目录,并将其追加到 GOPATH
中。
这是我的 .bashrc
。
export GOPATH=$HOME/go
export PATH=$PATH:/usr/local/go/bin:$GOPATH/bin
export GOPATH=$GOPATH:$HOME/all_projects/go # 添加项目目录
另一个需要注意的是,Go 更喜欢特定的目录结构。例如,如果你正在开发一个托管在 github.com 上的项目,你可以有以下目录结构。
go
|-bin
|-pkg
|-src
|---github.com
|-----username
|-------reponame
这个 YouTube 视频 在设置 VS Code 用于 Go 时非常有帮助。
英文:
Adding to @sadlil's answer, you can have a compound GOPATH
. You might have a separate directory where you have all your projects and you want to keep this directory structure. Create a go directory in your all_projects
directory and append it to the GOPATH
.
This is my .bashrc
.
export GOPATH=$HOME/go
export PATH=$PATH:/usr/local/go/bin:$GOPATH/bin
export GOPATH=$GOPATH:$HOME/all_projects/go # add projects directory
Another point to note is that go prefers a specific directory structure. For e.g. if you are working on a project you host at github.com, you can have the following directory structure.
go
|-bin
|-pkg
|-src
|---github.com
|-----username
|-------reponame
This youtube video was very helpful in setting up VS Code for go.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论