What should be the values of GOPATH and GOROOT?

huangapple go评论76阅读模式
英文:

What should be the values of GOPATH and GOROOT?

问题

goinstall: os: go/build: 本地找不到该包
goinstall: fmt: go/build: 本地找不到该包
goinstall: io: go/build: 本地找不到该包
goinstall: reflect: go/build: 本地找不到该包
goinstall: math: go/build: 本地找不到该包
goinstall: rand: go/build: 本地找不到该包
goinstall: url: go/build: 本地找不到该包
goinstall: net: go/build: 本地找不到该包
goinstall: sync: go/build: 本地找不到该包
goinstall: runtime: go/build: 本地找不到该包
goinstall: strings: go/build: 本地找不到该包
goinstall: sort: go/build: 本地找不到该包
goinstall: strconv: go/build: 本地找不到该包
goinstall: bytes: go/build: 本地找不到该包
goinstall: log: go/build: 本地找不到该包
goinstall: encoding/binary: go/build: 本地找不到该包

英文:

I'm trying to install doozer like this:

$ goinstall github.com/ha/doozer

I get these errors.

> goinstall: os: go/build: package could not be found locally
> goinstall: fmt: go/build: package could not be found locally
> goinstall: io: go/build: package could not be found locally
> goinstall: reflect: go/build: package could not be found locally
> goinstall: math: go/build: package could not be found locally
> goinstall: rand: go/build: package could not be found locally
> goinstall: url: go/build: package could not be found locally
> goinstall: net: go/build: package could not be found locally
> goinstall: sync: go/build: package could not be found locally
> goinstall: runtime: go/build: package could not be found locally
> goinstall: strings: go/build: package could not be found locally
> goinstall: sort: go/build: package could not be found locally
> goinstall: strconv: go/build: package could not be found locally
> goinstall: bytes: go/build: package could not be found locally
> goinstall: log: go/build: package could not be found locally
> goinstall: encoding/binary: go/build: package could not be found locally

答案1

得分: 417

GOPATHcmd/go文档中讨论:

GOPATH环境变量列出了查找Go代码的位置。在Unix上,该值是一个以冒号分隔的字符串。在Windows上,该值是一个以分号分隔的字符串。在Plan 9上,该值是一个列表。

必须设置GOPATH以获取、构建和安装标准Go树之外的软件包。

GOROOT安装说明中讨论:

Go二进制发行版假设它们将被安装在/usr/local/go(或Windows下的c:\Go)中,但也可以将Go工具安装到其他位置。在这种情况下,必须设置GOROOT环境变量,指向安装目录。

例如,如果将Go安装到主目录,则应将以下命令添加到$HOME/.profile中:

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

**注意:**只有在安装到自定义位置时才需要设置GOROOT

Chris Bunch的回答的更新版本。)

英文:

GOPATH is discussed in the cmd/go documentation:

> The GOPATH environment variable lists places to look for Go code. On
> Unix, the value is a colon-separated string. On Windows, the value is
> a semicolon-separated string. On Plan 9, the value is a list.
>
> GOPATH must be set to get, build and install packages outside the
> standard Go tree.

GOROOT is discussed in the installation instructions:

> The Go binary distributions assume they will be installed in
> /usr/local/go (or c:\Go under Windows), but it is possible to install
> the Go tools to a different location. In this case you must set the
> GOROOT environment variable to point to the directory in which it was
> installed.
>
> For example, if you installed Go to your home directory you should add
> the following commands to $HOME/.profile:
>
> export GOROOT=$HOME/go
> export PATH=$PATH:$GOROOT/bin
>
> Note: GOROOT must be set only when installing to a custom location.

(updated version of Chris Bunch's answer.)

答案2

得分: 136

这是我的简单设置:

与go相关的目录:~/programming/go
go编译器/工具的目录:~/programming/go/go-1.4
go软件的目录:~/programming/go/packages

GOROOT、GOPATH和PATH的设置如下:

export GOROOT=/home/user/programming/go/go-1.4
export GOPATH=/home/user/programming/go/packages
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin

简而言之:

GOROOT用于来自go安装的编译器/工具。
GOPATH用于您自己的go项目/第三方库(使用"go get"下载)。

英文:

Here is a my simple setup:

directory for go related things: ~/programming/go
directory for go compiler/tools: ~/programming/go/go-1.4
directory for go software      : ~/programming/go/packages

GOROOT, GOPATH, PATH are set as following:

export GOROOT=/home/user/programming/go/go-1.4
export GOPATH=/home/user/programming/go/packages
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin

So, in short:

GOROOT is for compiler/tools that comes from go installation.<br/>
GOPATH is for your own go projects / 3rd party libraries (downloaded with "go get").

答案3

得分: 93

首先运行 go env
如果你看到 go 没有安装,你可以通过 homebrew 或者其他方式安装它。
如果你看到输出结果,那么你的 Go 已经安装成功了。
它会显示所有已设置和未设置的环境变量。

如果你看到 GOROOT 是空的:

  1. 运行 which go(在我的电脑上是 /usr/local/go/bin/go
  2. 然后像这样导出 export GOROOT=/usr/local/go

如果你看到 GOPATH 是空的:

  1. 在你的电脑上的任意位置创建一个目录用于存放 Go 项目,比如我的情况是 ~/GO_PROJECTS
  2. 然后 export GOPATH=~/GO_PROJECTS
英文:

First run go env.
If you see that the go isn't installed, you can install it via homebrew or via package and/or other ways.
If you are seeing output then your Go is installed.
It shows you all the envs that are set and are not.

If you see empty for GOROOT:

  1. Run which go (On my computer : /usr/local/go/bin/go)
  2. then export like this export GOROOT=/usr/local/go

If you see empty for GOPATH:

  1. Create any directory anywhere on your computer for go projects in my case: ~/GO_PROJECTS
  2. Then export GOPATH=~/GO_PROJECTS

答案4

得分: 43

GOPATH在这里讨论:

> GOPATH环境变量
>
> GOPATH可以设置为一个由冒号分隔的路径列表,其中可以找到Go代码、包对象和可执行文件。
>
> 设置GOPATH以使用goinstall构建和安装自己的代码和外部库,这些库位于Go树之外(并且避免编写Makefile)。

GOROOT在这里讨论:

> $GOROOT是Go树的根目录,通常是$HOME/go。默认情况下,它是在运行all.bash的目录的父目录。如果您选择不设置$GOROOT,则在使用传统的makefile开发Go程序时,必须运行gomake而不是make或gmake。

英文:

GOPATH is discussed here:

> The GOPATH Environment Variable
>
> GOPATH may be set to a colon-separated list of paths inside which Go
> code, package objects, and executables may be found.
>
> Set a GOPATH to use goinstall to build and install your own code and
> external libraries outside of the Go tree (and to avoid writing
> Makefiles).

And GOROOT is discussed here:

> $GOROOT The root of the Go tree, often $HOME/go. This defaults to the
> parent of the directory where all.bash is run. If you choose not to
> set $GOROOT, you must run gomake instead of make or gmake when
> developing Go programs using the conventional makefiles.

答案5

得分: 20

我阅读了go help gopath文档,但仍然感到非常困惑,但在另一个go文档页面上找到了这个小提示:

> GOPATH环境变量指定了你的工作区的位置。这很可能是你在开发Go代码时需要设置的唯一环境变量。

http://golang.org/doc/code.html#GOPATH

英文:

I read the go help gopath docs and was still incredibly confused, but found this little nugget from another go doc page:

> The GOPATH environment variable specifies the location of your workspace. It is likely the only environment variable you'll need to set when developing Go code.

http://golang.org/doc/code.html#GOPATH

答案6

得分: 14

从go 1.8(2017年第二季度)开始,默认情况下会为您设置GOPATH为$HOME/go

参见问题17262Rob Pike的评论

> $HOME/go 就是它了。
没有单一的最佳答案,但这个答案简洁明了,只有当$HOME/go已经存在时才会选择这个名称,这只会发生在已经安装了go并且了解GOPATH的专家身上。

英文:

Starting with go 1.8 (Q2 2017), GOPATH will be set for you by default to $HOME/go

See issue 17262 and Rob Pike's comment:

> $HOME/go it will be.
There is no single best answer but this is short and sweet, and it can only be a problem to choose that name if $HOME/go already exists, which will only happen for experts who already have go installed and will understand GOPATH.

答案7

得分: 12

GOPATH不应该指向Go安装位置,而应该指向您的工作区(参见https://golang.org/doc/code.html#GOPATH)。每当您使用go get或go install安装某个包时,它都会安装到GOPATH中。这就是为什么它警告您,您绝对不希望将来自互联网的随机包倾倒到您的官方安装中。

英文:

The GOPATH should not point to the Go installation, but rather to your workspace (see https://golang.org/doc/code.html#GOPATH). Whenever you install some package with go get or go install, it will land within the GOPATH. That is why it warns you, that you most definitely do not want random packages from the internet to be dumped into your official installation.

答案8

得分: 12

通常情况下,你不需要显式设置GOROOTgo命令会根据自身的目录位置自动识别适当的GOROOT


GOPATH默认为$HOME/go。只有当你想将其放在其他位置时,才需要显式设置它。

GOPATH包含以下内容:

  • 使用go install安装的二进制文件,位于$GOPATH/bin。¹
    • 可以使用GOBIN环境变量来覆盖此位置。
  • 存放已下载的模块源代码和校验和的缓存,位于$GOPATH/pkg/mod
    • 可以使用GOMODCACHE环境变量来覆盖此位置。

如果同时设置了GOBINGOMODCACHE,并且未设置GO111MODULE=off,那么GOPATH本身基本上不会产生任何影响。


此外,在传统的GOPATH模式下(当GO111MODULE=off也被设置时),GOPATH还包含:

  • 用于构建包的源代码,存储在以$GOPATH/src为根目录的目录树中。
  • 使用go install安装的非二进制文件,位于$GOPATH/pkg
    • 安装非二进制包不再特别有用:go命令具有构建和测试缓存,即使在GOPATH模式下,自Go 1.12起就已经要求
    • 构建缓存不位于GOPATH内。可以使用GOCACHE环境变量来设置其位置。

¹ 在Go 1.17及之前的版本中,也可以使用go get来安装二进制文件,但自Go 1.16起,推荐使用go install;请参阅https://golang.org/doc/go1.16。

英文:

You generally should not set GOROOT explicitly. The go command identifies the appropriate GOROOT automatically based on its own directory location.


GOPATH defaults to $HOME/go. You only need to set it explicitly if you want to put it somewhere else.

GOPATH contains:

  • Binaries installed using go install, located at $GOPATH/bin
    • This location can be overridden using the GOBIN environment variable.
  • A cache of downloaded module source code and checksums, located at $GOPATH/pkg/mod.
    • This location can be overridden using the GOMODCACHE environment variable.

If you set both GOBIN and GOMODCACHE, and do not set GO111MODULE=off, then GOPATH itself should have essentially no effect.


In addition, in the legacy GOPATH mode (when GO111MODULE=off is also set), GOPATH contains:

  • Source code used to build packages, stored in a directory tree rooted at $GOPATH/src.
  • Non-binaries installed using go install, located at $GOPATH/pkg.
    • Installing non-binary packages is no longer particularly useful: the go command has a cache of built artifacts, which has been required since Go 1.12 even in GOPATH mode.
    • The build cache is not located within GOPATH. Its location can be set with the GOCACHE environment variable.

¹ Binaries can also be installed using go get on Go 1.17 and earlier, but go install is preferred as of Go 1.16; see https://golang.org/doc/go1.16.

答案9

得分: 8

关于GOROOT,Go 1.9将自动将其设置为安装路径。即使您安装了多个Go版本,调用1.9.x版本将会将GOROOT设置为/path/to/go/1.9(之前,如果未设置,它会假设默认路径如/usr/local/goc:\Go)。

参见CL Go Review 53370

> go tool现在将使用调用它的路径来尝试定位Go安装树的根目录
这意味着如果整个Go安装被移动到新位置,go tool应该会像往常一样继续工作。

> 这可以通过在环境中设置GOROOT来覆盖,但这只应在特殊情况下进行。
请注意,这不会影响runtime.GOROOT()函数的结果,它将继续报告原始安装位置;这可能会在以后的版本中修复。

英文:

Regarding GOROOT specifically, Go 1.9 will set it automatically to its installation path.
Even if you have multiple Go installed, calling the 1.9.x one will set GOROOT to /path/to/go/1.9 (before, if not set, it assumed a default path like /usr/local/go or c:\Go).

See CL Go Review 53370:

> The go tool will now use the path from which it was invoked to attempt to locate the root of the Go install tree.
This means that if the entire Go installation is moved to a new location, the go tool should continue to work as usual.

> This may be overriden by setting GOROOT in the environment, which should only be done in unusual circumstances.
Note that this does not affect the result of the runtime.GOROOT() function, which will continue to report the original installation location; this may be fixed in later releases.

答案10

得分: 8

在现代的Go中,你不需要设置GOPATHGOROOT。实际上,除非你在做一些非常专业的事情,最好在你的系统上将它们取消设置。

使用Go模块安装了Go之后,选择一个你想要工作的目录。然后:

$ mkdir example
$ cd example
$ go mod init example.com

注意,模块名example.com是任意的;如果你将你的工作放在GitHub上,可以是类似github.com/your-username/project-name的东西。

最后一个命令会创建一个go.mod文件;现在你可以用go get来获取依赖项:

$ go get rsc.io/quote

现在你的代码使用了这个依赖项:

$ touch main.go

将以下内容放在main.go中:

package main

import (
	"fmt"

	"rsc.io/quote"
)

func main() {
	fmt.Println(quote.Go())
}

然后运行:

$ go run .

关于原始问题,你现在可以使用以下命令获取你的doozer依赖项:

$ go get github.com/ha/doozer

现在你可以在你的代码中使用doozer模块。以此类推。你还可以检查你的目录中的go.mod文件,查看列出的依赖项以及它们的版本。每个模块都是自包含的,有自己的依赖项版本。你可以在一起拥有两个模块,每个模块都有自己的go.mod文件,指向某个依赖项的不同版本 - 这一切都能正常工作,因为模块之间是隔离的。

有关更多信息,请从这里的官方教程开始。在几个章节中,它会引导你完成上面显示的步骤,以及编写自己的可重用模块和包,并从其他模块导入它们。还可以在https://play-with-go.dev/上找到其他交互式教程。

英文:

In modern Go, you don't need to set GOPATH or GOROOT. In fact, unless you're doing something very specialized, it's best to have them unset on your system.

Use Go modules. Having installed Go, pick a directory where you want to work. Then:

$ mkdir example
$ cd example
$ go mod init example.com

Note that the module name example.com is arbitrary; if you keep your work on GitHub, this could be something like github.com/your-username/project-name.

The last command will have created a go.mod file; now you can grab dependencies with go get:

$ go get rsc.io/quote

Now your code using this dependency:

$ touch main.go

Place this in main.go:

package main

import (
	&quot;fmt&quot;

	&quot;rsc.io/quote&quot;
)

func main() {
	fmt.Println(quote.Go())
}

And run with:

$ go run .

W.r.t. original question, you can now get your doozer dependency with:

$ go get github.com/ha/doozer

Now you can use the doozer module in your code. And so on. You can also examine the go.mod file in your directory to see the dependencies listed, along with their versions. Each module is self-contained, with its own versions of dependencies. You can have two modules alongside each other, each with its own go.mod file pointing to different versions of some dependency - this will all work OK because of the isolation between modules.

For additional information, start with the official tutorial here. In several chapters, it walks you through the steps shown above, as well as writing your own reusable modules and packages, and importing them from other modules. Additional interactive tutorials are available at https://play-with-go.dev/

答案11

得分: 8

GOPATHGOROOT配置已被弃用。

您可以使用GO模块代替。

例如:

mkdir go_app
cd go_app
go mod init go_app
英文:

GOPATH and GOROOT configurations are deprecated.

You can use the GO module instead.

For example:

mkdir go_app
cd go_app
go mod init go_app

答案12

得分: 6

如上所述:

> GOPATH环境变量指定了您的工作区的位置。

对于Windows,我在Ms-dos窗口中使用以下命令成功:

set GOPATH=D:\my_folder_for_go_code\

这将创建一个Ms-dos可以识别的GOPATH变量,可以在使用时按如下方式使用:

cd %GOPATH%
英文:

As mentioned above:

> The GOPATH environment variable specifies the location of your
> workspace.

For Windows, this worked for me (in Ms-dos window):

set GOPATH=D:\my_folder_for_go_code\

This creates a GOPATH variable that Ms-dos recognizes when used as follows:

cd %GOPATH%

答案13

得分: 5

很多答案,但没有实质内容,就像机器人在他们的系统上进行剪切和粘贴一样。没有必要将GOROOT设置为环境变量。然而,有一个有益的需要设置GOPATH环境变量,如果没有设置,默认为${HOME}/go/文件夹。

你必须注意的是PATH环境变量,因为这个变量可以改变你的go版本,而不是GOROOT!忘记GOROOT

现在,如果你切换或更改到一个新的go版本,你下载的包将使用默认的$HOME/go文件夹,并且会与你之前的go版本混合在一起。这不好。

因此,在这里,你需要定义GOPATH以隔离新go版本的下载包。

总之,忘记GOROOT。更多地考虑GOPATH

英文:

Lots of answers but no substance, like robots doing cut and paste on what's on their system. There is no need to set GOROOT as an environment variable. However, there is a beneficial need to set the GOPATH environment variable, and if not set it defaults to ${HOME}/go/ folder.

It is the PATH environment variable that you must pay attention because this variable is the variable that can change your go version. Not GOROOT! Forget GOROOT.

Now, if you switch or change to a new go version, your downloaded packages will use the default $HOME/go folder and it will mixed-up with whatever your previous go version was. This is not good.

Therefore, this is where GOPATH you need to define in order to isolate downloaded packages of the new go version.

In summary, forget GOROOT. Think more on GOPATH.

答案14

得分: 2

运行 go help environment,它包含了可以通过 go env 命令列出的每个环境变量的文档。

英文:

Run go help environment it has documentation for every environment variable that can be listed by go env command

答案15

得分: 2

有一个命令可以使用:go env GOPATH

英文:

There's a command you can use: go env GOPATH

答案16

得分: 1

这里是一个解决方案(单用户):

GOROOT=$HOME/.local # 你的go可执行文件在$GOROOT/bin中
GOPATH=$HOME/.gopath
PATH=$GOROOT/bin:$GOPATH/bin:$PATH

如果你将.gopath改为.gogo会抱怨。

我希望他们能像rust/cargo的开发者那样,把所有东西都放在一个地方。

英文:

Here is one solution (single user):

GOROOT=$HOME/.local # your go executable is in $GOROOT/bin
GOPATH=$HOME/.gopath
PATH=$GOROOT/bin:$GOPATH/bin:$PATH

go complains if you change .gopath to .go.

I wish they went with how the rust/cargo guys did and just put everything at one place.

答案17

得分: 0

你不需要显式设置GOROOT(现代版本的Go可以根据运行的go二进制文件的位置自动找到它)。

此外,当尝试使用vgo时,遇到了以下错误:

go: modules disabled inside GOPATH/src by GO111MODULE=auto; see 'go help modules'

移除GOROOT,更新我的GOPATH并且export GO111MODULE="on"解决了这个问题。

这里可以查看有关GOPATH的信息。

> GOPATH可以设置为一个由冒号分隔的路径列表,其中可以找到Go代码、包对象和可执行文件。
>
> 设置GOPATH以使用goinstall来构建和安装自己的代码和Go树之外的外部库(并避免编写Makefile)。

英文:

You don't need to explicitly set GOROOT (Modern versions of Go can figure it out on their own based on the location of the go binary that you run).

Also, got the follow error when trying to work with vgo:

go: modules disabled inside GOPATH/src by GO111MODULE=auto; see &#39;go help modules&#39;

Removing GOROOT, updating my GOPATH and export GO111MODULE=&quot;on&quot; resolved the issue.

GOPATH see in here

> GOPATH may be set to a colon-separated list of paths inside which Go code, package objects, and executables may be found.
>
> Set a GOPATH to use goinstall to build and install your own code and external libraries outside of the Go tree (and to avoid writing Makefiles).

答案18

得分: 0

截至2020年和Go版本1.13+,在Windows上更新GOPATH的最佳方法是在命令提示符中键入以下命令:

setx GOPATH C:\mynewgopath
英文:

As of 2020 and Go version 1.13+, in Windows the best way for updating GOPATH is just typing in command prompt:

setx GOPATH C:\mynewgopath

答案19

得分: -1

我不需要翻译的内容是:

export GOROOT=/usr/local/Cellar/go/1.10.1/libexec

to my ~/.bash_profile on Mac OS X

英文:

I had to append

export GOROOT=/usr/local/Cellar/go/1.10.1/libexec

to my ~/.bash_profile on Mac OS X

答案20

得分: -1

还有一种情况是当我们使用go时,它会编译所有的go文件。

假设我们有一个文件main.go,然后我们将当前文件更改为main_old.go,然后添加了新的main.go文件。然后当我们构建我们的应用程序时,所有的go文件都会被编译。所以发生的错误可能是由于其他go文件中的编译错误。

英文:

There is also a case where when we use go it compiles all the go files.

So lets say we had one file main.go and later we changed the current file to main_old.go and then added our new main.go file. Then when we build our app all the go files will get compiled. So the error that's happening might be due to compilation error in some other go files.

答案21

得分: -1

一旦安装了Go语言,GOROOT就是安装的根目录。

当我将Go语言二进制文件解压到Windows的C:\目录中时,我的GOROOT应该是C:\go。
如果使用Windows安装程序安装,则可能是C:\Program Files\go(或C:\Program Files(x86)\go,用于64位软件包)。

 GOROOT = C:\go

而我的GOPATH是Go语言源代码或工作区的位置。

如果我的Go语言源代码位于C:\Users<xyz>\GO_Workspace,那么你的GOPATH应该如下所示:

 GOPATH = C:\Users\<xyz>\GO_Workspace
英文:

Once Go lang is installed, GOROOT is the root directory of the installation.

When I exploded Go Lang binary in Windows C:\ directory, my GOROOT should be C:\go.
If Installed with Windows installer, it may be C:\Program Files\go (or C:\Program Files (x86)\go, for 64-bit packages)

 GOROOT = C:\go

while my GOPATH is location of Go lang source code or workspace.

If my Go lang source code is located at C:\Users&lt;xyz>\GO_Workspace, your GOPATH would be as below:

 GOPATH = C:\Users\&lt;xyz&gt;\GO_Workspace

答案22

得分: -1

对于所有新手来说,如果你使用的是Ubuntu,可以简单地执行export GOPATH=$HOME/go,如果需要更多信息,请执行go help gopath

英文:

For all newcomers they could do a simply export GOPATH=$HOME/go if you are using Ubuntu or do go help gopath for more information.

答案23

得分: -2

在OSX上,我使用brew进行安装,这是适用于我的设置:

GOPATH="$HOME/my_go_work_space" //确保你已经创建了这个文件夹

GOROOT="/usr/local/Cellar/go/1.10/libexec"
英文:

in osx, i installed with brew, here is the setting that works for me

GOPATH=&quot;$HOME/my_go_work_space&quot; //make sure you have this folder created

GOROOT=&quot;/usr/local/Cellar/go/1.10/libexec&quot;

答案24

得分: -3

如果您使用的是发行版go,您应该指向包含文件的位置,例如:

$ rpm -ql golang | grep include
/usr/lib/golang/include

(这是针对Fedora 20的)

英文:

If you are using the distro go, you should point to where the include files are, for example:

$ rpm -ql golang | grep include
/usr/lib/golang/include

(This is for Fedora 20)

答案25

得分: -3

值应该是(MACOS):

GOROOT = "/usr/local/go" -> 所有二进制文件核心go
GOPATH = "/Applications/proyectos/go" -> 工作区的路径(自定义工作区)
英文:

the values should be (MACOS):

GOROOT=&quot;/usr/local/go&quot; --&gt; all binaries file core go
GOPATH=&quot;/Applications/proyectos/go&quot; --&gt; the route to workspace (custom workspace)

huangapple
  • 本文由 发表于 2011年11月2日 01:32:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/7970390.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定