如何在Ubuntu上设置GOPATH环境变量?我要编辑什么文件?

时间:2021-11-27 23:08:19

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:

当我进行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?

我看到很多线程都提到了这个错误,但是没有一个线程能够回答我的问题,需要编辑哪个文件才能为这个路径提供值?

20 个解决方案

#1


218  

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.

只需在~/中添加以下几行。bashrc将继续存在。但是,您可以使用其他路径,如GOPATH,而不是$HOME/进入我的示例。

export GOPATH=$HOME/go
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin

#2


60  

Ubuntu 14.04

Ubuntu 14.04

export GOPATH=$HOME/go

Additionally you can add this string to file

此外,还可以将该字符串添加到文件中

$HOME/.bashrc

#3


48  

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 "workspace",它下载包,等等。我用~ / .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/

#4


17  

If for example, it is an Ubuntu, after installing the packages:

例如,它是一个Ubuntu,安装包后:

$sudo apt install golang -y

Just add the following lines to ~/.bashrc (Of your user)

只需在~/中添加以下几行。bashrc(用户)(

export GOROOT=/usr/lib/go
export GOPATH=$HOME/go
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin

#5


10  

Shortest and current solution.

最短的和当前的解决方案。

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.

要持久,请根据shell将下面的代码粘贴到.zshrc或.bashrc文件中。你的主目录中的那些文件。如下图所示。

export PATH=path/to/some/other/place/composer/for/example
export GOPATH=path/to/your/gopath/directory
export PATH=$PATH:$GOPATH/bin

#6


10  

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环境变量。

#7


8  

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
  • 键入sudo nano /etc/environment
  • find PATH=... and go the end of it and add a colon : after the last path then paste in your full go path e.g. /home/user/gocode
  • 找到路径=…然后在末尾添加一个冒号:在最后一条路径之后,在你的完整路径中粘贴,例如/home/user/gocode。

and you're done, This should make it persistent through the system.

你做完了,这应该会让它在系统中持续。

#8


6  

You will need GOPATH later, too. So add it to ~/.bashrc.

你以后也需要GOPATH。所以将它添加到~/.bashrc中。

#9


4  

This is what got it working for me on Ubuntu 15.10 using the fish shell:

这就是我在Ubuntu 15.10上使用鱼壳的原因:

# ~/.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)

然后我必须更改go文件夹的权限(它设置为root)

sudo chown <name>:<name> -R /usr/share/go

#10


3  

With Go 1.8 (Q2 2017), you won't have to edit any file if you accept the default GOPATH value (set for you)

使用Go 1.8(2017年第二季度),如果您接受默认的GOPATH值(为您设置),您就不必编辑任何文件

$HOME/go

You can see the comments on issue 17262 and the associated twitter vote:

你可以看到关于17262号问题的评论和相关的推特投票:

By choosing a default GOPATH, we can make our documentation easier because we can say things like

通过选择一个默认的GOPATH,我们可以使我们的文档更容易,因为我们可以这样说

$ go get github.com/foo/bar

will check out the github.com/foo/bar repo into $HOME/go/src/github.com/foo/bar.

将查看github.com/foo/bar repo到$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

我们不需要通过设置env var来分散新手的注意力,我们需要做的就是在页面底部放一个括号

$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.

$HOME/go是go工作区的默认路径。如果您想改变这条路径,请将GOPATH变量设置为您所选择的内容。

#11


3  

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.

go path可以是您想要的任何位置,只需创建一个目录并将全局路径变量以GOPATH的名义设置到您的环境中。

mkdir ~/go
export GOPATH=~/go
go get github.com/go-sql-driver/mysql

#12


3  

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.

我决定将GOPATH设置为/ usr/lib/goi。试一下。

export GOPATH=/usr/lib/go
export PATH=$PATH:$GOPATH/bin

#13


3  

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.

GOPATH是工作空间位置的环境变量。GOROOT是安装目录的环境变量。虽然GOROOT和GOPATH在安装时是自动设置的(如果没有bug),但是要手动指定它,可以遵循下面的过程。此外,更多信息你可以参考GO的wiki页面。

It is better to set GOPATH to a directory inside your home directory, e.g., $HOME/go, %USERPROFILE%\go (Windows).

最好将GOPATH设置为您的主目录中的目录,例如$ home /go, %USERPROFILE%\go (Windows)。

  1. 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.
  2. 这是一个mac的解决方案,在macOS Sierra测试。10.12,在Gogland-EAP中,JetBrains已经将其作为Go编程的IDE引入。

On your Terminal type

在你的终端类型

vim ~/.profile

in opened document on the Terminal press i and add the following path

在打开的文件中,终端按i并添加以下路径。

GOPATH=/path/to/a/directory/inside/home/directory
GOROOT=/path/to/you/go/library
PATH=$PATH:$GOPATH:$GOROOT:$GOROOT/bin

press ESC and type :x. Lastly, you should restart (close and open) your terminal or Logout and Login again.

按ESC和type:x。最后,您应该重新启动(关闭和打开)您的终端或注销和再次登录。

  1. For Windows and Linux configuration, please refer to Go wiki page at Githab on Setting GOPATH-Golang.
  2. 关于Windows和Linux的配置,请参考Githab关于设置GOPATH-Golang的wiki页面。

CAUTION Do not set both GOROOT and GOPATH to the same directory, otherwise you will get a warning.

注意,不要将GOROOT和GOPATH都设置为同一个目录,否则会收到警告。

#14


0  

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是作为根用户安装的。系统bash_profile文本文件的根用户~/。bash_profile需要将$GOROOT分配到go安装目录,将$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:$ HOME / bin:/ usr /地方/ bin:/ usr /地方/去/ 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)

退出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.

现在你可以在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。这是另一个冒险…:)

#15


0  

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

#16


0  

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.

GOPATH环境变量指定工作空间的位置。它默认为一个名为进入你的家目录下的目录,所以$ home / Unix,$ home / Plan 9,和% USERPROFILE % \ Windows上的(通常是C:\Users\YourName\走)。如果您想在不同的位置工作,需要将GOPATH设置为该目录的路径。(另一个常见的设置是设置GOPATH=$HOME。)注意GOPATH不能与Go安装的路径相同。

So for example, if you are coding in Jetbrains Webstorm (using the Go plugin), you might want to set GOPATH as /Users/<user>/WebstormProjects.

例如,如果您正在Jetbrains Webstorm中编写代码(使用Go插件),您可能需要将GOPATH设置为/Users/ /WebstormProjects。

In simpler words, set it to wherever you want your Go projects to reside.

简单地说,将它设置为您希望Go项目驻留的任何地方。

#17


0  

Yet another solution: Export every GO* environment variable from go env

另一个解决方案是:从GO env导出每个GO*环境变量

.bashrc:

. 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"

#18


0  

Edit your ~/.bash_profile to add the following line:

编辑你的~ /。bash_profile添加如下行:

$ export GOPATH=$HOME/work

Save and exit your editor. Then, source your ~/.bash_profile

保存并退出编辑器。然后,来源你的~ / . bash_profile

$ source ~/.bash_profile

Note: Set the GOBIN path to generate a binary file when go install is run

注意:设置GOBIN路径,以便在go安装运行时生成二进制文件

$ export GOBIN=$HOME/work/bin

#19


0  

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.

你可以像其他人建议的那样使用“导出”解决方案。我想为您提供另一个永久方便的解决方案:当运行Go命令时,您可以使用任何路径作为GOPATH。

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).

首先,您需要下载一个名为gost的小工具:https://github.com/byte16/gost/release。如果您使用ubuntu,您可以下载linux版本(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:

你会得到一个可执行的gost。您可以将其移动到/usr/local/bin,方便使用:

$ 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.

运行下面的命令,将要用作GOPATH的路径添加到pathspace gost维护中。需要为路径提供一个稍后使用的名称。

$ 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:

以以下格式运行任何Go命令:

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:

例如,你想要运行go get github.com/go-sql-driver/mysql,以/home/foobar/bar作为GOPATH,就这样做吧:

$ 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 :

它将帮助您设置GOPATH并运行命令。但是请记住,您已经将路径添加到gost的路径空间中。如果您处于/home/foobar/bar的任何级别的子目录下,您甚至可以运行下面的命令,该命令将执行同样的操作:

$ 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是一个简单的Go工具,可以帮助您管理gop誓言和运行Go命令。有关如何使用它运行其他Go命令的详细信息,您可以运行gost help goCmdName。例如,你想了解更多关于安装的信息,只需在下面输入以下单词:

$ gost help install

You can also find more details in the README of the project: https://github.com/byte16/gost/blob/master/README.md

您还可以在项目的自述中找到更多细节:https://github.com/byte16/gost/blob/master/README.md

#20


-1  

This script will help you switch Gopaths. https://github.com/buffonomics/goswitch

这个脚本将帮助您切换gop誓言。https://github.com/buffonomics/goswitch

#1


218  

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.

只需在~/中添加以下几行。bashrc将继续存在。但是,您可以使用其他路径,如GOPATH,而不是$HOME/进入我的示例。

export GOPATH=$HOME/go
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin

#2


60  

Ubuntu 14.04

Ubuntu 14.04

export GOPATH=$HOME/go

Additionally you can add this string to file

此外,还可以将该字符串添加到文件中

$HOME/.bashrc

#3


48  

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 "workspace",它下载包,等等。我用~ / .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/

#4


17  

If for example, it is an Ubuntu, after installing the packages:

例如,它是一个Ubuntu,安装包后:

$sudo apt install golang -y

Just add the following lines to ~/.bashrc (Of your user)

只需在~/中添加以下几行。bashrc(用户)(

export GOROOT=/usr/lib/go
export GOPATH=$HOME/go
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin

#5


10  

Shortest and current solution.

最短的和当前的解决方案。

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.

要持久,请根据shell将下面的代码粘贴到.zshrc或.bashrc文件中。你的主目录中的那些文件。如下图所示。

export PATH=path/to/some/other/place/composer/for/example
export GOPATH=path/to/your/gopath/directory
export PATH=$PATH:$GOPATH/bin

#6


10  

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环境变量。

#7


8  

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
  • 键入sudo nano /etc/environment
  • find PATH=... and go the end of it and add a colon : after the last path then paste in your full go path e.g. /home/user/gocode
  • 找到路径=…然后在末尾添加一个冒号:在最后一条路径之后,在你的完整路径中粘贴,例如/home/user/gocode。

and you're done, This should make it persistent through the system.

你做完了,这应该会让它在系统中持续。

#8


6  

You will need GOPATH later, too. So add it to ~/.bashrc.

你以后也需要GOPATH。所以将它添加到~/.bashrc中。

#9


4  

This is what got it working for me on Ubuntu 15.10 using the fish shell:

这就是我在Ubuntu 15.10上使用鱼壳的原因:

# ~/.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)

然后我必须更改go文件夹的权限(它设置为root)

sudo chown <name>:<name> -R /usr/share/go

#10


3  

With Go 1.8 (Q2 2017), you won't have to edit any file if you accept the default GOPATH value (set for you)

使用Go 1.8(2017年第二季度),如果您接受默认的GOPATH值(为您设置),您就不必编辑任何文件

$HOME/go

You can see the comments on issue 17262 and the associated twitter vote:

你可以看到关于17262号问题的评论和相关的推特投票:

By choosing a default GOPATH, we can make our documentation easier because we can say things like

通过选择一个默认的GOPATH,我们可以使我们的文档更容易,因为我们可以这样说

$ go get github.com/foo/bar

will check out the github.com/foo/bar repo into $HOME/go/src/github.com/foo/bar.

将查看github.com/foo/bar repo到$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

我们不需要通过设置env var来分散新手的注意力,我们需要做的就是在页面底部放一个括号

$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.

$HOME/go是go工作区的默认路径。如果您想改变这条路径,请将GOPATH变量设置为您所选择的内容。

#11


3  

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.

go path可以是您想要的任何位置,只需创建一个目录并将全局路径变量以GOPATH的名义设置到您的环境中。

mkdir ~/go
export GOPATH=~/go
go get github.com/go-sql-driver/mysql

#12


3  

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.

我决定将GOPATH设置为/ usr/lib/goi。试一下。

export GOPATH=/usr/lib/go
export PATH=$PATH:$GOPATH/bin

#13


3  

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.

GOPATH是工作空间位置的环境变量。GOROOT是安装目录的环境变量。虽然GOROOT和GOPATH在安装时是自动设置的(如果没有bug),但是要手动指定它,可以遵循下面的过程。此外,更多信息你可以参考GO的wiki页面。

It is better to set GOPATH to a directory inside your home directory, e.g., $HOME/go, %USERPROFILE%\go (Windows).

最好将GOPATH设置为您的主目录中的目录,例如$ home /go, %USERPROFILE%\go (Windows)。

  1. 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.
  2. 这是一个mac的解决方案,在macOS Sierra测试。10.12,在Gogland-EAP中,JetBrains已经将其作为Go编程的IDE引入。

On your Terminal type

在你的终端类型

vim ~/.profile

in opened document on the Terminal press i and add the following path

在打开的文件中,终端按i并添加以下路径。

GOPATH=/path/to/a/directory/inside/home/directory
GOROOT=/path/to/you/go/library
PATH=$PATH:$GOPATH:$GOROOT:$GOROOT/bin

press ESC and type :x. Lastly, you should restart (close and open) your terminal or Logout and Login again.

按ESC和type:x。最后,您应该重新启动(关闭和打开)您的终端或注销和再次登录。

  1. For Windows and Linux configuration, please refer to Go wiki page at Githab on Setting GOPATH-Golang.
  2. 关于Windows和Linux的配置,请参考Githab关于设置GOPATH-Golang的wiki页面。

CAUTION Do not set both GOROOT and GOPATH to the same directory, otherwise you will get a warning.

注意,不要将GOROOT和GOPATH都设置为同一个目录,否则会收到警告。

#14


0  

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是作为根用户安装的。系统bash_profile文本文件的根用户~/。bash_profile需要将$GOROOT分配到go安装目录,将$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:$ HOME / bin:/ usr /地方/ bin:/ usr /地方/去/ 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)

退出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.

现在你可以在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。这是另一个冒险…:)

#15


0  

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

#16


0  

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.

GOPATH环境变量指定工作空间的位置。它默认为一个名为进入你的家目录下的目录,所以$ home / Unix,$ home / Plan 9,和% USERPROFILE % \ Windows上的(通常是C:\Users\YourName\走)。如果您想在不同的位置工作,需要将GOPATH设置为该目录的路径。(另一个常见的设置是设置GOPATH=$HOME。)注意GOPATH不能与Go安装的路径相同。

So for example, if you are coding in Jetbrains Webstorm (using the Go plugin), you might want to set GOPATH as /Users/<user>/WebstormProjects.

例如,如果您正在Jetbrains Webstorm中编写代码(使用Go插件),您可能需要将GOPATH设置为/Users/ /WebstormProjects。

In simpler words, set it to wherever you want your Go projects to reside.

简单地说,将它设置为您希望Go项目驻留的任何地方。

#17


0  

Yet another solution: Export every GO* environment variable from go env

另一个解决方案是:从GO env导出每个GO*环境变量

.bashrc:

. 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"

#18


0  

Edit your ~/.bash_profile to add the following line:

编辑你的~ /。bash_profile添加如下行:

$ export GOPATH=$HOME/work

Save and exit your editor. Then, source your ~/.bash_profile

保存并退出编辑器。然后,来源你的~ / . bash_profile

$ source ~/.bash_profile

Note: Set the GOBIN path to generate a binary file when go install is run

注意:设置GOBIN路径,以便在go安装运行时生成二进制文件

$ export GOBIN=$HOME/work/bin

#19


0  

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.

你可以像其他人建议的那样使用“导出”解决方案。我想为您提供另一个永久方便的解决方案:当运行Go命令时,您可以使用任何路径作为GOPATH。

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).

首先,您需要下载一个名为gost的小工具:https://github.com/byte16/gost/release。如果您使用ubuntu,您可以下载linux版本(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:

你会得到一个可执行的gost。您可以将其移动到/usr/local/bin,方便使用:

$ 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.

运行下面的命令,将要用作GOPATH的路径添加到pathspace gost维护中。需要为路径提供一个稍后使用的名称。

$ 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:

以以下格式运行任何Go命令:

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:

例如,你想要运行go get github.com/go-sql-driver/mysql,以/home/foobar/bar作为GOPATH,就这样做吧:

$ 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 :

它将帮助您设置GOPATH并运行命令。但是请记住,您已经将路径添加到gost的路径空间中。如果您处于/home/foobar/bar的任何级别的子目录下,您甚至可以运行下面的命令,该命令将执行同样的操作:

$ 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是一个简单的Go工具,可以帮助您管理gop誓言和运行Go命令。有关如何使用它运行其他Go命令的详细信息,您可以运行gost help goCmdName。例如,你想了解更多关于安装的信息,只需在下面输入以下单词:

$ gost help install

You can also find more details in the README of the project: https://github.com/byte16/gost/blob/master/README.md

您还可以在项目的自述中找到更多细节:https://github.com/byte16/gost/blob/master/README.md

#20


-1  

This script will help you switch Gopaths. https://github.com/buffonomics/goswitch

这个脚本将帮助您切换gop誓言。https://github.com/buffonomics/goswitch