英文:
How do I SET the GOPATH environment variable on Ubuntu? What file must I edit?
问题
我正在尝试执行go get
命令:
go get github.com/go-sql-driver/mysql
但是它失败了,并显示以下错误:
package github.com/go-sql-driver/mysql: cannot download, $GOPATH not set. For more details see: go help gopath
当我执行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"
显然,GOPATH未设置,我应该在哪里设置它?
我看到很多帖子提到了这个错误,但没有一个提供了我问题的答案,哪个文件需要编辑以提供此路径的值?
英文:
I am trying to do a 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:
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?
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?
答案1
得分: 370
新的方法:
注意:这不适用于在主机上使用go install [repo url]
来尝试运行Go应用程序/二进制文件的情况,在这种情况下,您仍然需要使用旧的方法。
旧的方法:
只需将以下几行添加到~/.bashrc
文件中,这样就可以持久保存。但是,在我的示例中,您可以使用其他您喜欢的路径作为GOPATH,而不是$HOME/go
。
export GOPATH=$HOME/go
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
英文:
New Way:
Note: Not for trying out a go application / binaries on your host machine using go install [repo url]
, in such cases you still have to use the old 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/go
in my sample.
export GOPATH=$HOME/go
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
答案2
得分: 130
新的方式:Go Modules
自从Go 1.11版本以后,你不再需要使用GOPATH了。只需进入你的项目目录,执行以下命令:
go mod init github.com/youruser/yourrepo
- 这样,Go会在该目录下创建一个模块根目录。
- 你可以创建任意数量的模块。
- 如果需要逐步指导,请参考这个回答。
旧的方式:GOPATH
如果你坚持使用GOPATH,那么请注意以下内容:
- 自从Go 1.8版本以后,你不需要设置GOPATH或GOROOT。
- 默认情况下,GOPATH位于你的用户/主目录下。
> 如果没有设置GOPATH,Unix系统默认为$HOME/go,Windows系统默认为%USERPROFILE%\go。如果你想使用自定义位置作为工作区,可以设置GOPATH环境变量。
英文:
New Way: Go Modules
Since Go 1.11, you don't have to use GOPATH anymore. Simply go to your project directory and do this once:
go mod init github.com/youruser/yourrepo
- With this, Go creates a module root at that directory.
- You can create as many modules as you want.
- For step by step instructions, also see this answer.
Old Way: GOPATH
If you insist on working with GOPATH then heed this:
- Since Go 1.8, you don't need to set your GOPATH or GOROOT.
- GOPATH by default is under your user/home directory.
> If no GOPATH is set, it is assumed to be $HOME/go on Unix systems and %USERPROFILE%\go on Windows. If you want to use a custom location as your workspace, you can set the GOPATH environment variable.
答案3
得分: 68
Ubuntu 14.04
将以下内容添加到文件中:
export GOPATH=$HOME/go
此外,您还可以将以下字符串添加到文件中:
$HOME/.bashrc
英文:
Ubuntu 14.04
export GOPATH=$HOME/go
Additionally you can add this string to file
$HOME/.bashrc
答案4
得分: 65
GOPATH应设置为一个新创建的空目录。这是Go的"工作空间",用于下载包等。我使用~/.go。
例如:
mkdir ~/.go
echo "GOPATH=$HOME/.go" >> ~/.bashrc
echo "export GOPATH" >> ~/.bashrc
echo "PATH=$PATH:$GOPATH/bin # 将GOPATH/bin添加到PATH以供脚本使用" >> ~/.bashrc
source ~/.bashrc
来源:http://www.larry-price.com/blog/2013/12/15/setting-up-a-go-environment-in-ubuntu-12-dot-04/
英文:
GOPATH should be set to a newly created empty directory. This is the go "workspace", where it downloads packages, et cetera. I use ~/.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/
答案5
得分: 45
如果是Ubuntu系统,安装完软件包后,可以将以下内容添加到用户的~/.bashrc文件中:
export GOROOT=/usr/lib/go
export GOPATH=$HOME/go
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
英文:
If for example, it is an Ubuntu, after installing the packages:
$sudo apt install golang -y
Just add the following lines to ~/.bashrc (Of your user)
export GOROOT=/usr/lib/go
export GOPATH=$HOME/go
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
答案6
得分: 20
导出GOPATH=/path/desired/here
无需编辑任何文件,我们可以直接使用上述命令来编辑Go环境变量。
英文:
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.
答案7
得分: 20
请在终端中输入以下代码:
export GOPATH=path/to/your/gopath/directory
注意:这个设置会在每次打开新的终端窗口或系统重启时重置。
如果想要持久化这个设置,请根据你使用的shell,在你的.zshrc
或.bashrc
文件中添加以下代码。这些文件位于你的Home Directory(主目录)下。添加的代码如下:
export PATH=path/to/some/other/place/composer/for/example
export GOPATH=path/to/your/gopath/directory
export PATH=$PATH:$GOPATH/bin
英文:
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.
export PATH=path/to/some/other/place/composer/for/example
export GOPATH=path/to/your/gopath/directory
export PATH=$PATH:$GOPATH/bin
答案8
得分: 13
对于Go 1.13+版本:
go env -w GOPATH=$HOME/go
要设置GOPATH,无论GO版本如何,请将以下行添加到您的~/.bashrc
文件中:
export GOPATH=$HOME/go
并且不要忘记加载您的.bashrc
文件:
source ~/.bashrc
有关golang
官方维基上的更多选项:
https://github.com/golang/go/wiki/SettingGOPATH
英文:
For Go 1.13+:
go env -w GOPATH=$HOME/go
To set the GOPATH regardless of the GO version add the following line to your ~/.bashrc
:
export GOPATH=$HOME/go
and don't forget to source your .bashrc
file:
source ~/.bashrc
More options on the golang
official wiki:
https://github.com/golang/go/wiki/SettingGOPATH
答案9
得分: 12
你以后也会需要GOPATH,所以将其添加到~/.bashrc
中。
英文:
You will need GOPATH later, too. So add it to ~/.bashrc
.
答案10
得分: 8
如果你已经设置了需要修改环境变量的东西,比如Java、Go等,这将非常熟悉。
我假设你的Go路径在某个地方有以下目录结构:
\---[文件夹名称]
+---bin
+---pkg
\---src
- 打开一个新的终端
- 输入
sudo nano /etc/environment
- 找到
PATH=...
,在最后一个路径后面加上一个冒号:
,然后粘贴你的完整的go path
,例如/home/user/gocode
完成后,这将使其在系统中持久化。
英文:
If you've setup anything that needs modification of environment variables e.g. Java, Go etc this will be very familiar.
I will assume that you have the following directory structure somewhere as your Go path:
\---[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
and you're done, This should make it persistent through the system.
答案11
得分: 4
以下是要翻译的内容:
export GOROOT=/usr/lib/go
export GOPATH=$HOME/go
export PATH=$PATH:$GOROOT/bin
And you may want to check by
$ go env
请注意检查:
$ go env
英文:
export GOROOT=/usr/lib/go
export GOPATH=$HOME/go
export PATH=$PATH:$GOROOT/bin
And you may want to check by
$ go env
答案12
得分: 4
只需将以下几行代码添加到 ~/.bashrc 文件中:
export GOROOT=/usr/lib/go
export GOPATH=$HOME/go
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
英文:
Just add the following lines to ~/.bashrc
export GOROOT=/usr/lib/go
export GOPATH=$HOME/go
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
答案13
得分: 3
这是我在 Ubuntu 15.10 上使用 fish shell
使其工作的方法:
# ~/.config/fish/config.fish
set -g -x PATH /usr/local/bin $PATH
set -g -x GOPATH /usr/share/go
然后我需要更改 go
文件夹的权限(它被设置为 root)
sudo chown <name>:<name> -R /usr/share/go
英文:
This is what got it working for me on Ubuntu 15.10 using the fish shell
:
# ~/.config/fish/config.fish
set -g -x PATH /usr/local/bin $PATH
set -g -x GOPATH /usr/share/go
Then I had to change the permissions on the go
folder (it was set to root)
sudo chown <name>:<name> -R /usr/share/go
答案14
得分: 3
go path可以放在任何你想要的地方,只需创建一个目录,并将全局路径变量设置为GOPATH,以适应你的环境。
mkdir ~/go
export GOPATH=~/go
go get github.com/go-sql-driver/mysql
英文:
go path could be every where you want just create a directory and set global path variable in the name of GOPATH to your environment.
mkdir ~/go
export GOPATH=~/go
go get github.com/go-sql-driver/mysql
答案15
得分: 3
GOPATH 是一个指向工作空间位置的环境变量。
GOROOT 是一个指向安装目录的环境变量。虽然在安装过程中会自动设置GOROOT和GOPATH(如果没有错误的话),但你也可以手动指定它们,具体操作如下。此外,你可以参考GO的维基页面获取更多信息。
最好将GOPATH设置为你的主目录下的一个目录,例如$HOME/go(在Windows上为%USERPROFILE%\go)。
- 这是一个在macOS Sierra(版本10.12)和Gogland-EAP(由JetBrains推出的Go编程IDE)上测试过的解决方案。
在终端上输入以下命令:
vim ~/.profile
在打开的文档中按下i键,并添加以下路径:
GOPATH=/path/to/a/directory/inside/home/directory
GOROOT=/path/to/you/go/library
PATH=$PATH:$GOPATH:$GOROOT:$GOROOT/bin
按下ESC键,然后输入**:x保存并退出。
最后,你应该重新启动(关闭和打开**)终端,或者注销并重新登录。
- 关于Windows和Linux的配置,请参考Go维基页面上的设置GOPATH-Golang。
注意 不要将GOROOT和GOPATH都设置为同一个目录,否则会收到警告。
英文:
GOPATH is an environment variable to your work-space location.
GOROOT is an environment variable to your installation directory. Although GOROOT and GOPATH is automatically set (if there would not be a bug) during the installation time, to specify it manually you can follow below process. Moreover, for more information you can refer to GO's wiki page.
It is better to set GOPATH to a directory inside your home directory, e.g., $HOME/go, %USERPROFILE%\go (Windows).
- This is a solution mac, which is tested on macOS Sierra, ver. 10.12, and also in Gogland-EAP, which has been introduced as an IDE for Go programming by JetBrains.
On your Terminal type
vim ~/.profile
in opened document on the Terminal press i and add the following path
GOPATH=/path/to/a/directory/inside/home/directory
GOROOT=/path/to/you/go/library
PATH=$PATH:$GOPATH:$GOROOT:$GOROOT/bin
press ESC and type .
Lastly, you should restart (close and open) your terminal or Logout and Login again.
- For Windows and Linux configuration, please refer to Go wiki page at Githab on Setting GOPATH-Golang.
CAUTION Do not set both GOROOT and GOPATH to the same directory, otherwise you will get a warning.
答案16
得分: 3
(Ubuntu)
如果您没有设置GOPATH,将使用默认值。
您需要将$GOPATH/bin添加到您的PATH中,以执行在$GOPATH/bin中安装的任何二进制文件,或者您需要键入$GOPATH/bin/the-command。将以下内容添加到您的~/.bash_profile文件中:
export PATH=$GOPATH/bin:$PATH
当前的GOPATH命令:
go env GOPATH
更改GOPATH命令:
export GOPATH=$HOME/your-desired-path
英文:
(Ubuntu)
If you don’t set a GOPATH, the default will be used.
You have to add $GOPATH/bin to your PATH to execute any binary installed in $GOPATH/bin, or you need to type $GOPATH/bin/the-command.
Add this to your ~/.bash_profile
> export PATH=$GOPATH/bin:$PATH
Current GOPATH command:
> go env GOPATH
Changing the GOPATH command:
> export GOPATH=$HOME/your-desired-path
答案17
得分: 2
在Go 1.8(2017年第二季度)中,如果你接受默认的GOPATH
值(为你设置的),你将不需要编辑任何文件。
$HOME/go
你可以在issue 17262和相关的Twitter投票中看到评论:
> 通过选择默认的GOPATH
,我们可以使我们的文档更容易理解,因为我们可以这样说
$ go get github.com/foo/bar
> 将会把github.com/foo/bar
仓库检出到$HOME/go/src/github.com/foo/bar
。
> 我们不需要让新手分心去设置环境变量,我们只需要在页面底部加上一个小括号的注释
> > $HOME/go
是你的Go工作区的默认路径。
如果你想要更改这个路径,请将GOPATH
变量设置为你选择的路径。
英文:
With Go 1.8 (Q2 2017), you won't have to edit any file if you accept the default GOPATH
value (set for you)
$HOME/go
You can see the comments on issue 17262 and the associated twitter vote:
> By choosing a default GOPATH
, we can make our documentation easier because we can say things like
$ go get github.com/foo/bar
> will check out the github.com/foo/bar
repo into $HOME/go/src/github.com/foo/bar
.
> We don't need to distract newcomers with having to set an env var, all we need to do is put a little parenthetical note at the bottom of the page
> > $HOME/go
is the default path to your go workspace.
If you want to change this path, set the GOPATH
variable to something of your choosing.
答案18
得分: 2
我的Go环境看起来与你的类似。
$go env
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH=""
GORACE=""
GOROOT="/usr/lib/go-1.6"
GOTOOLDIR="/usr/lib/go-1.6/pkg/tool/linux_amd64"
GO15VENDOREXPERIMENT="1"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0"
CXX="g++"
CGO_ENABLED="1"
我通过将GOPATH设置为**/usr/lib/go**来解决了这个问题。你可以试试。
export GOPATH=/usr/lib/go
export PATH=$PATH:$GOPATH/bin
英文:
My go environment looked similar to yours.
$go env
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH=""
GORACE=""
GOROOT="/usr/lib/go-1.6"
GOTOOLDIR="/usr/lib/go-1.6/pkg/tool/linux_amd64"
GO15VENDOREXPERIMENT="1"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0"
CXX="g++"
CGO_ENABLED="1"
I resolved it with setting GOPATH to /usr/lib/go. Try it out.
export GOPATH=/usr/lib/go
export PATH=$PATH:$GOPATH/bin
答案19
得分: 1
在~/.profile文件的末尾添加以下内容:
export GOPATH="$HOME/go"
export PATH="$PATH:/usr/local/go/bin"
export PATH="$PATH:$GOPATH/bin"
英文:
At the end of the ~.profile file add::
export GOPATH="$HOME/go"
export PATH="$PATH:/usr/local/go/bin"
export PATH="$PATH:$GOPATH/bin"
答案20
得分: 1
如果你正在使用zsh:
nano ~/.zshrc
然后在文件末尾添加:
export PATH=$PATH:/usr/local/go/bin
最后:
source ~/.zshrc
并打开一个新的终端:
go version
英文:
if you are using zsh :
nano ~/.zshrc
then add to the end of the file :
export PATH=$PATH:/usr/local/go/bin
finally :
source ~/.zshrc
and open a new terminal
go version
答案21
得分: 1
将以下代码翻译为中文:
export PATH=$PATH:$(go env GOPATH)/bin
英文:
export PATH=$PATH:$(go env GOPATH)/bin
答案22
得分: 1
对于Ubuntu系统:
在.bashrc文件中添加以下路径,并保存文件:
export PATH=$PATH:/usr/local/go/bin:$HOME/go/bin
英文:
For Ubuntu:
Add the following path in .bashrc file and save the file
export PATH=$PATH:/usr/local/go/bin:$HOME/go/bin
答案23
得分: 0
这是最令人讨厌的事情之一。为了节省您的时间。
如果go是以root用户安装的。您系统的bash_profile文本文件~/.bash_profile需要将$GOROOT分配给go安装目录,并将$GOPATH分配给go/src目录。
...$# sudo su
...$# vi ~/.bash_profile
***在vi编辑器中继续故事***
GOROOT=$GOROOT:/usr/local/go
GOPATH=$GOPATH:/usr/local/go/src
...
[在此处添加您的常规PATH内容]
...
确保go二进制文件的路径在.bash_profile中
PATH=$PATH:$HOME/bin:/usr/local/bin:/usr/local/go/bin
这个PATH可以是任意长度的字符串..要添加新项,只需用冒号 : 分隔:
退出vi编辑器并保存bash配置文件 (:wq 表示写入并退出)
[esc]
[shift] + [:]
:wq
您必须退出终端并重新登录以重新启动配置文件..或者您可以使用export命令启动它。
...$# export GOPATH=/usr/local/go/src
您可以验证go环境:
...$# go env
太棒了!
GOBIN=""
GOCHAR="6"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/usr/local/go/src"
GORACE=""
GOROOT="/usr/local/go"
现在您可以使用sudo命令,go将能够在go/src中下载和创建目录,您可以继续您想要完成的工作。
例如:
# sudo go get github.com/..
现在您可能会遇到另一个问题..您可能没有安装git..那是另一个冒险..:)
英文:
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.
...$# 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
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 :
exit vi editor and saving bash profile (:wq stands for write and quit)
[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
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.
example
# sudo go get github.com/..
Now you will run into another problem..you might not have git installed..that's another adventure..:)
答案24
得分: 0
在我的 Fedora 20 机器上,我在我的 ~/.bashrc 文件中添加了以下几行代码:
export GOROOT=/usr/lib/golang
export GOPATH=$HOME/go
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
英文:
On my Fedora 20 machine I added the following lines to my ~/.bashrc:
export GOROOT=/usr/lib/golang
export GOPATH=$HOME/go
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
答案25
得分: 0
另一种解决方案:从go env
导出每个GO*
环境变量。
.bashrc:
eval $(go env | grep '^GO[A-Z0-9_]*=' | while read setenv; do
echo "export $setenv; "
done 2> /dev/null)
[[ -n $GOPATH ]] || export GOPATH="$HOME/go/bin"
[[ -n $GOROOT ]] || export GOROOT=/usr/bin/go
export PATH="$PATH:$GOPATH/bin:$GOROOT/bin"
英文:
Yet another solution: Export every GO*
environment variable from go env
.bashrc:
<!-- language: bash -->
eval $(go env | grep '^GO[A-Z0-9_]*=' | while read setenv; do
echo "export $setenv; "
done 2> /dev/null)
[[ -n $GOPATH ]] || export GOPATH="$HOME/go/bin"
[[ -n $GOROOT ]] || export GOROOT=/usr/bin/go
export PATH="$PATH:$GOPATH/bin:$GOROOT/bin"
答案26
得分: 0
请编辑您的~/.bash_profile
文件,添加以下行:
$ export GOPATH=$HOME/work
保存并退出编辑器。然后,执行以下命令来加载~/.bash_profile
:
$ source ~/.bash_profile
注意:设置GOBIN路径以在运行go install
时生成二进制文件:
$ export GOBIN=$HOME/work/bin
英文:
Edit your ~/.bash_profile
to add the following line:
$ export GOPATH=$HOME/work
Save and exit your editor. Then, source your ~/.bash_profile
$ source ~/.bash_profile
Note: Set the GOBIN path to generate a binary file when go install is run
$ export GOBIN=$HOME/work/bin
答案27
得分: -1
这个脚本将帮助你切换Gopath。你可以在这个链接中找到它:https://github.com/buffonomics/goswitch
英文:
This script will help you switch Gopaths. https://github.com/buffonomics/goswitch
答案28
得分: -1
你可以像其他人建议的那样使用"export"解决方案。我想给你提供另一种方便的解决方案:在运行Go命令时,你可以使用任何路径作为GOPATH。
首先,你需要下载一个名为"gost"的小工具:https://github.com/byte16/gost/releases。如果你使用的是Ubuntu,你可以下载Linux版本(https://github.com/byte16/gost/releases/download/v0.1.0/gost_linux_amd64.tar.gz)。
然后,你需要运行下面的命令来解压它:
$ cd /path/to/your/download/directory
$ tar -xvf gost_linux_amd64.tar.gz
你会得到一个可执行文件"gost"。你可以将它移动到"/usr/local/bin"以方便使用:
$ sudo mv gost /usr/local/bin
运行下面的命令将你想要用作GOPATH的路径添加到"gost"维护的"pathspace"中。你需要给路径一个名称,以便以后使用。
$ gost add foo /home/foobar/bar # 'foo'是名称,'/home/foobar/bar'是路径
以以下格式运行任何你想要的Go命令:
gost goCommand [-p {pathName}] -- [goFlags...] [goArgs...]
例如,如果你想要以"/home/foobar/bar"作为GOPATH运行"go get github.com/go-sql-driver/mysql",只需按照以下方式执行:
$ gost get -p foo -- github.com/go-sql-driver/mysql # 'foo'是你给路径的名称。
它将帮助你设置GOPATH并运行命令。但请记住,你已经将路径添加到"gost"的"pathspace"中。如果你在"/home/foobar/bar"的任何子目录级别下,甚至可以只运行以下命令,它将以简短的方式完成相同的操作:
$ gost get -- github.com/go-sql-driver/mysql
"gost"是一个简单的Go工具,可以帮助你管理GOPATH并运行Go命令。关于如何使用它运行其他Go命令的更多详细信息,你可以运行"gost help goCmdName"。例如,如果你想了解更多关于"install"的信息,只需输入以下命令:
$ gost help install
你还可以在项目的README中找到更多详细信息:https://github.com/byte16/gost/blob/master/README.md
英文:
You can use the "export" solution just like what other guys have suggested. I'd like to provide you with another solution for permanent convenience: you can use any path as GOPATH when running Go commands.
Firstly, you need to download a small tool named gost
: https://github.com/byte16/gost/releases . If you use ubuntu, you can download the linux version(https://github.com/byte16/gost/releases/download/v0.1.0/gost_linux_amd64.tar.gz).
Then you need to run the commands below to unpack it :
$ cd /path/to/your/download/directory
$ tar -xvf gost_linux_amd64.tar.gz
You would get an executable gost
. You can move it to /usr/local/bin
for convenient use:
$ sudo mv gost /usr/local/bin
Run the command below to add the path you want to use as GOPATH into the pathspace gost
maintains. It is required to give the path a name which you would use later.
$ gost add foo /home/foobar/bar # 'foo' is the name and '/home/foobar/bar' is the path
Run any Go command you want in the format:
gost goCommand [-p {pathName}] -- [goFlags...] [goArgs...]
For example, you want to run go get github.com/go-sql-driver/mysql
with /home/foobar/bar
as the GOPATH, just do it as below:
$ gost get -p foo -- github.com/go-sql-driver/mysql # 'foo' is the name you give to the path above.
It would help you to set the GOPATH and run the command. But remember that you have added the path into gost
's pathspace. If you are under any level of subdirectories of /home/foobar/bar
, you can even just run the command below which would do the same thing for short :
$ gost get -- github.com/go-sql-driver/mysql
gost
is a Simple Tool of Go which can help you to manage GOPATHs and run Go commands. For more details about how to use it to run other Go commands, you can just run gost help goCmdName
. For example you want to know more about install
, just type words below in:
$ gost help install
You can also find more details in the README of the project: https://github.com/byte16/gost/blob/master/README.md
答案29
得分: -1
你需要根据你使用的终端(bash或zsh)来更新PATH。
-
打开终端的shell脚本文件,即
~/.bashrc
或~/.zshrc
,在编辑器中打开。vi ~/.zshrc
(或)
code ~/.zshrc -
如果已经存在,更新下面的GOPATH,否则添加下面的行。
export GOPATH=$HOME/go:/$HOME/projects/go
在这里,你可以通过分号
:
将系统中不同位置的GO项目的路径添加到GOPATH
环境变量中,例如/path/1:path/2:path/3
等等。在我的情况下,我添加了两个路径,如上所示,一个是从根目录
$HOME/go
,另一个是从项目目录:/$HOME/projects/go
。
英文:
You have to update the PATH based on the terminal(bash or zsh) which you use.
- Open the shell script file of the terminal i.e
~/.bashrc
or~/.zshrc
in an editor
> vi ~/.zshrc
> (or)
> code ~/.zshrc
- Update the below GOPATH if already found or add the below line.
export GOPATH=$HOME/go:/$HOME/projects/go
Here you can add one or more paths separated by a semicolon :
from different locations of your GO projects on the system to the GOPATH
environment variable i.e /path/1:path/2:path/3
etc.
In my case, I have added 2 paths, as shown above, one from the root $HOME/go
and the other one from the projects directory :/$HOME/projects/go
答案30
得分: -2
根据官方说明所写:
GOPATH环境变量指定了工作区的位置。它默认为一个名为go的目录,位于您的主目录下,因此在Unix上为$HOME/go,在Plan 9上为$home/go,在Windows上为%USERPROFILE%\go(通常为C:\Users\YourName\go)。如果您想在其他位置工作,您需要将GOPATH设置为该目录的路径。(另一种常见的设置是将GOPATH设置为$HOME。)请注意,GOPATH不能与您的Go安装路径相同。
因此,例如,如果您在Jetbrains Webstorm中编码(使用Go插件),您可能希望将GOPATH
设置为/Users/<user>/WebstormProjects
。
简单来说,将其设置为您希望存放Go项目的位置。
英文:
As written in the official instructions:
> The GOPATH environment variable specifies the location of your workspace. It defaults to a directory named go inside your home directory, so $HOME/go on Unix, $home/go on Plan 9, and %USERPROFILE%\go (usually C:\Users\YourName\go) on Windows. If you would like to work in a different location, you will need to set GOPATH to the path to that directory. (Another common setup is to set GOPATH=$HOME.) Note that GOPATH must not be the same path as your Go installation.
So for example, if you are coding in Jetbrains Webstorm (using the Go plugin), you might want to set GOPATH
as /Users/<user>/WebstormProjects
.
In simpler words, set it to wherever you want your Go projects to reside.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论