Linux 如何在 Ubuntu 上设置 GOPATH 环境变量?我必须编辑什么文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21001387/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I SET the GOPATH environment variable on Ubuntu? What file must I edit?
提问by David Saintloth
I am trying to do a go get
:
我正在尝试做一个go get
:
go get github.com/go-sql-driver/mysql
and it fails with the following error:
它失败并出现以下错误:
package github.com/go-sql-driver/mysql: cannot download, $GOPATH not set. For more details see: go help gopath
when I do a go env
, a list of Go values is shown as below:
当我执行 a 时go env
,Go 值列表如下所示:
ubuntu@ip-xxx-x-xx-x:~$ go env
GOARCH="amd64"
GOBIN=""
GOCHAR="6"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH=""
GORACE=""
GOROOT="/usr/lib/go"
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_amd64"
CC="gcc"
GOGCCFLAGS="-g -O2 -fPIC -m64 -pthread"
CGO_ENABLED="1"
clearly the GOPATH is not set, how and where do I set it?
显然 GOPATH 没有设置,我应该如何以及在哪里设置它?
I see many threads that mention this error but none that provide an answer to my question, which file needs to be edited to provide a value for this path?
我看到很多线程都提到了这个错误,但没有一个线程可以回答我的问题,需要编辑哪个文件才能为该路径提供值?
回答by David Saintloth
export GOPATH=/path/desired/here
There is no need to edit any file, we can just use the command above to directly edit the Go environment variables.
不需要编辑任何文件,我们可以直接使用上面的命令编辑Go环境变量。
回答by Vytautas Shaltenis
You will need GOPATH later, too. So add it to ~/.bashrc
.
稍后您也将需要 GOPATH。所以将其添加到~/.bashrc
.
回答by ymg
If you've setup anything that needs modification of environment variables e.g. Java, Go etc this will be very familiar.
如果您已经设置了任何需要修改环境变量的东西,例如 Java、Go 等,这将是非常熟悉的。
I will assume that you have the following directory structure somewhere as your Go path:
我将假设您在某处具有以下目录结构作为您的 Go 路径:
\---[folder name]
+---bin
+---pkg
\---src
- open a new terminal
- type
sudo nano /etc/environment
- find
PATH=...
and go the end of it and add a colon:
after the last path then paste in your fullgo path
e.g./home/user/gocode
- 打开一个新终端
- 类型
sudo nano /etc/environment
- 找到
PATH=...
并转到它的末尾并:
在最后一条路径后添加一个冒号,然后粘贴完整的go path
例如/home/user/gocode
and you're done, This should make it persistent through the system.
大功告成,这应该让它在系统中持久化。
回答by Daniel Lin
New Way:
新方法:
Old Way:
旧方式:
Just add the following lines to ~/.bashrc and this will persist. However, you can use other paths you like as GOPATH instead of $HOME/goin my sample.
只需将以下行添加到 ~/.bashrc 中,这将持续存在。但是,在我的示例中,您可以使用其他您喜欢的路径作为 GOPATH 而不是$HOME/go。
export GOPATH=$HOME/go
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
回答by Bryan Larsen
GOPATH should be set to a newly created empty directory. This is the go "workspace", where it downloads packages, et cetera. I use ~/.go.
GOPATH 应设置为新创建的空目录。这是 go“工作区”,它下载软件包等。我使用 ~/.go。
For example:
例如:
mkdir ~/.go
echo "GOPATH=$HOME/.go" >> ~/.bashrc
echo "export GOPATH" >> ~/.bashrc
echo "PATH=$PATH:$GOPATH/bin # Add GOPATH/bin to PATH for scripting" >> ~/.bashrc
source ~/.bashrc
source: http://www.larry-price.com/blog/2013/12/15/setting-up-a-go-environment-in-ubuntu-12-dot-04/
来源:http: //www.larry-price.com/blog/2013/12/15/setting-up-a-go-environment-in-ubuntu-12-dot-04/
回答by pasha.zhukov
Ubuntu 14.04
Ubuntu 14.04
export GOPATH=$HOME/go
Additionally you can add this string to file
此外,您可以将此字符串添加到文件
$HOME/.bashrc
回答by user3151532
This has been the most annoying thing to deal with. In hopes of saving your time.
这是最烦人的事情。希望能节省您的时间。
IF go was installed as root user. The root user of your system's bash_profile text file ~/.bash_profile needs to have $GOROOT assigned to the go install directory and $GOPATH needs to be assigned to go /src directory.
如果 go 以 root 用户身份安装。系统的 bash_profile 文本文件 ~/.bash_profile 的 root 用户需要将 $GOROOT 分配给 go install 目录,并且需要将 $GOPATH 分配给 go /src 目录。
...$# sudo su
...$# vi ~/.bash_profile
***Story continues in vi editor***
GOROOT=$GOROOT:/usr/local/go
GOPATH=$GOPATH:/usr/local/go/src
...
[your regular PATH stuff here]
...
be sure the path to go binary is in your path on .bash_profile
确保二进制文件的路径在您的 .bash_profile 路径中
PATH=$PATH:$HOME/bin:/usr/local/bin:/usr/local/go/bin
PATH=$PATH:$HOME/bin:/usr/local/bin:/usr/local/go/bin
This PATH can be as long a string it needs to be..to add new items just separate by colon :
这个 PATH 可以是它需要的字符串长度..添加新项目只是用冒号分隔:
exit vi editor and saving bash profile (:wq stands for write and quit)
退出 vi 编辑器并保存 bash 配置文件(:wq 代表写入和退出)
[esc]
[shift] + [:]
:wq
You have to log out of terminal and log back in for profile to initiate again..or you can just kick start it by using export.
您必须退出终端并重新登录以重新启动配置文件......或者您可以使用导出来启动它。
...$# export GOPATH=/usr/local/go/src
You can verify go env:
您可以验证 go env:
...$# go env
Yay!
好极了!
GOBIN=""
GOCHAR="6"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/usr/local/go/src"
GORACE=""
GOROOT="/usr/local/go"
Now you can sudo and go will be able to download and create directories inside go/src and you can get down to what you were trying to get done.
现在你可以 sudo 并且 go 将能够在 go/src 中下载和创建目录,你可以开始做你想要完成的事情。
example
例子
# sudo go get github.com/..
Now you will run into another problem..you might not have git installed..that's another adventure..:)
现在你会遇到另一个问题..你可能没有安装git..那是另一个冒险..:)
回答by Raptor
On my Fedora 20 machine I added the following lines to my ~/.bashrc:
在我的 Fedora 20 机器上,我在 ~/.bashrc 中添加了以下几行:
export GOROOT=/usr/lib/golang
export GOPATH=$HOME/go
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
回答by Glstunna
This script will help you switch Gopaths. https://github.com/buffonomics/goswitch
此脚本将帮助您切换 Gopath。https://github.com/buffonomics/goswitch
回答by atilkan
Write this code in Terminal.
在终端中编写此代码。
export GOPATH=path/to/your/gopath/directory
Note: This will reset on every new Terminal window or system restart.
注意:这将在每个新的终端窗口或系统重新启动时重置。
To be persistent, paste the code below in your .zshrc
or .bashrc
file according to your shell. Those files in your Home Directory. It will be like below.
要保持持久性,请根据您的外壳将以下代码粘贴到您的.zshrc
或.bashrc
文件中。您的主目录中的那些文件。它将如下所示。
export PATH=path/to/some/other/place/composer/for/example
export GOPATH=path/to/your/gopath/directory
export PATH=$PATH:$GOPATH/bin