我正在尝试像这样安装 doozer :
$ goinstall github.com/ha/doozer
我收到这些错误。
goinstall: os: go/build: 本地找不到包
goinstall: fmt: go/build: 本地找不到包
goinstall: io: go/build: 本地找不到包
goinstall: 反映: go/build: 本地找不到包
goinstall:数学:go/build:本地找不到包
goinstall: rand: go/build: 本地找不到包
goinstall: url: go/build: 本地找不到包
goinstall: net: go/build: 本地找不到包
goinstall:同步:go/build:本地找不到包
goinstall:运行时:go/build:本地找不到包
goinstall:字符串:go/build:本地找不到包
goinstall: 排序: go/build: 本地找不到包
goinstall: strconv: go/build: 本地找不到包
goinstall:字节:go/build:本地找不到包
goinstall:日志:go/build:本地找不到包
goinstall:编码/二进制: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
发布评论
评论(25)
GOPATH
在cmd/go
中进行了讨论文档:将
GOPATH
设置为用于安装下载的软件包的自定义目录。GOROOT
在 安装说明:从 CY2023 开始,对于任何现代 Go 安装,不要设置/导出
GOROOT
< /strong>.(Chris Bunch 的更新版本回答。)
GOPATH
is discussed in thecmd/go
documentation:Set
GOPATH
to a custom directory for installing downloaded packages.GOROOT
is discussed in the installation instructions:As of CY2023, for any modern Go installation, do not set/export
GOROOT
.(updated version of Chris Bunch's answer.)
这是我的简单设置:
GOROOT、GOPATH、PATH 设置如下:
所以,简而言之:
GOROOT 用于来自 go 安装的编译器/工具。
GOPATH 适用于您自己的 go 项目/第 3 方库(使用“go get”下载)。
Here is a my simple setup:
GOROOT, GOPATH, PATH are set as following:
So, in short:
GOROOT is for compiler/tools that comes from go installation.
GOPATH is for your own go projects / 3rd party libraries (downloaded with "go get").
首先运行
go env
。如果您发现 go 尚未安装,您可以通过
homebrew
或通过包和/或其他方式安装它。如果您看到输出,则说明您的
Go
已安装。它显示了所有已设置和未设置的环境。
如果您看到
GOROOT
为空:which go
(在我的计算机上:/usr/local/go/bin/go
)export GOROOT=/usr/local/go
如果您看到
GOPATH
为空:~/ GO_PROJECTS
导出 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
:which go
(On my computer :/usr/local/go/bin/go
)export GOROOT=/usr/local/go
If you see empty for
GOPATH
:~/GO_PROJECTS
export GOPATH=~/GO_PROJECTS
此处讨论了
GOPATH
:此处讨论了
GOROOT
:GOPATH
is discussed here:And
GOROOT
is discussed here:我阅读了
go help 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:http://golang.org/doc/code.html#GOPATH
您通常不应显式设置 GOROOT。
go
命令根据其自己的目录位置自动识别适当的GOROOT
。GOPATH
默认为$HOME/go
。如果你想把它放在其他地方,你只需要明确地设置它。GOPATH
包含:go install
安装的二进制文件,位于$GOPATH/bin
。GOBIN
环境变量覆盖此位置。$GOPATH/pkg /mod
。GOMODCACHE
环境变量覆盖此位置。如果您同时设置了
GOBIN
和GOMODCACHE
,并且没有设置GO111MODULE=off
,那么GOPATH
本身应该本质上已经没有效果。此外,在旧版 GOPATH 模式下(当还设置了 GO111MODULE=off 时),GOPATH 包含:
$GOPATH/src
为根的目录树中。go install
安装的非二进制文件,位于$GOPATH/pkg
。go
命令具有 构建工件的缓存,自 Go 以来一直需要它1.12 即使在GOPATH
模式下也是如此。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. Thego
command identifies the appropriateGOROOT
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:go install
, located at$GOPATH/bin
.¹GOBIN
environment variable.$GOPATH/pkg/mod
.GOMODCACHE
environment variable.If you set both
GOBIN
andGOMODCACHE
, and do not setGO111MODULE=off
, thenGOPATH
itself should have essentially no effect.In addition, in the legacy
GOPATH
mode (whenGO111MODULE=off
is also set),GOPATH
contains:$GOPATH/src
.go install
, located at$GOPATH/pkg
.go
command has a cache of built artifacts, which has been required since Go 1.12 even inGOPATH
mode.GOPATH
. Its location can be set with theGOCACHE
environment variable.¹ Binaries can also be installed using
go get
on Go 1.17 and earlier, butgo install
is preferred as of Go 1.16; see https://golang.org/doc/go1.16.从 go 1.8(2017 年第 2 季度)开始,GOPATH 将默认设置为 $HOME/go
请参阅 问题 17262 和 Rob Pike 的评论:
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:
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.
具体到
GOROOT
,Go 1.9会自动将其设置为安装路径。即使您安装了多个 Go,调用 1.9.x 也会将
GOROOT
设置为/path/to/go/1.9
(之前,如果没有设置,它会假定默认路径,例如/usr/local/go
或c:\Go
)。请参阅 CL Go 评论 53370:
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
orc:\Go
).See CL Go Review 53370:
在现代 Go 中,您不需要设置
GOPATH
或GOROOT
。事实上,除非您正在做一些非常专业的事情,否则最好在您的系统上取消设置它们。使用 Go 模块。 安装 Go 后,选择您想要工作的目录。然后:
注意模块名称
example.com
是任意的;如果您将工作保存在 GitHub 上,则可能类似于 github.com/your-username/project-name。最后一个命令将创建一个 go.mod 文件;现在您可以使用
go get
获取依赖项:现在您的代码使用此依赖项:
将其放入
main.go
:并运行:
Wrt 原始问题,您现在可以获取您的
doozer
依赖项:现在您可以在代码中使用
doozer
模块。等等。您还可以检查目录中的go.mod
文件,以查看列出的依赖项及其版本。每个模块都是独立的,具有自己的依赖版本。您可以拥有两个并排的模块,每个模块都有自己的 go.mod 文件,指向某些依赖项的不同版本 - 由于模块之间的隔离,这一切都可以正常工作。有关更多信息,请从此处的官方教程开始。在几个章节中,它将引导您完成上面所示的步骤,以及编写您自己的可重用模块和包,以及从其他模块导入它们。如需其他交互式教程,请访问 https://play-with-go.dev/
In modern Go, you don't need to set
GOPATH
orGOROOT
. 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:
Note that the module name
example.com
is arbitrary; if you keep your work on GitHub, this could be something likegithub.com/your-username/project-name
.The last command will have created a
go.mod
file; now you can grab dependencies withgo get
:Now your code using this dependency:
Place this in
main.go
:And run with:
W.r.t. original question, you can now get your
doozer
dependency with:Now you can use the
doozer
module in your code. And so on. You can also examine thego.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 owngo.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/
GOPATH
和GOROOT
配置已弃用。您可以改用 GO 模块。
例如:
GOPATH
andGOROOT
configurations are deprecated.You can use the GO module instead.
For example:
如上所述:
对于 Windows,这对我有用(在 Ms-dos 窗口中):
这将创建一个 GOPATH 变量,Ms-dos 在使用时可以识别该变量,如下所示:
As mentioned above:
For Windows, this worked for me (in Ms-dos window):
This creates a GOPATH variable that Ms-dos recognizes when used as follows:
有很多答案,但没有实质内容,就像机器人在系统上剪切和粘贴内容一样。无需将 GOROOT 设置为环境变量。但是,有一个有益需要设置
GOPATH
环境变量,如果不设置,则默认为${HOME}/go/
文件夹。你必须注意的是
PATH
环境变量,因为这个变量是可以改变你的go版本
的变量。不是GOROOT
!忘记GOROOT
。现在,如果您切换或更改到新的
go 版本
,您下载的软件包将使用默认的$HOME/go
文件夹,并且它将与您之前的
是。这不好。$HOME/go
文件夹混合在一起。 code>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 theGOPATH
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 yourgo version
. NotGOROOT
! ForgetGOROOT
.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 previousgo version
was. This is not good.Therefore, this is where
GOPATH
you need to define in order to isolate downloaded packages of the newgo version
.In summary, forget
GOROOT
. Think more onGOPATH
.运行
go helpenvironment
,它包含可以通过go env
命令列出的每个环境变量的文档Run
go help environment
it has documentation for every environment variable that can be listed bygo env
command您可以使用一个命令:
go env GOPATH
There's a command you can use:
go env GOPATH
这是一种解决方案(单用户):
如果将
.gopath
更改为.go
,go
会抱怨。我希望他们能效仿 rust/cargo 的做法,将所有东西都放在一个地方。
Here is one solution (single user):
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.您不需要显式设置 GOROOT(Go 的现代版本可以根据您运行的 go 二进制文件的位置自行计算)。
此外,在尝试使用
vgo
时出现以下错误:删除 GOROOT、更新我的 GOPATH 和
export GO111MODULE="on"
解决了该问题。GOPATH 参见此处
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
:Removing GOROOT, updating my GOPATH and
export GO111MODULE="on"
resolved the issue.GOPATH see in here
截至 2020 年和 Go 版本 1.13+,在 Windows 中更新 GOPATH 的最佳方法只需在命令提示符中键入:
As of 2020 and Go version 1.13+, in Windows the best way for updating GOPATH is just typing in command prompt:
我必须附加
到 Mac OS X 上的 ~/.bash_profile
I had to append
to my ~/.bash_profile on Mac OS X
还有一种情况,当我们使用 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.
一旦安装了Go lang,GOROOT就是安装的根目录。
当我在 Windows C:\ 目录中分解 Go Lang 二进制文件时,我的 GOROOT 应该是 C:\go。
如果使用 Windows 安装程序安装,则可能是 C:\Program Files\go(或 C:\Program Files (x86)\go,对于 64 位软件包),
而我的 GOPATH 是 Go lang 源代码或工作区的位置。
如果我的 Go lang 源代码位于 C:\Users\\GO_Workspace,则您的 GOPATH 将如下所示:
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)
while my GOPATH is location of Go lang source code or workspace.
If my Go lang source code is located at C:\Users\\GO_Workspace, your GOPATH would be as below:
对于所有新手,如果您使用 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 dogo help gopath
for more information.在osx中,我安装了brew,这是适合我的设置
in osx, i installed with brew, here is the setting that works for me
如果您使用发行版 go,您应该指出包含文件的位置,例如:(
适用于 Fedora 20)
If you are using the distro go, you should point to where the include files are, for example:
(This is for Fedora 20)
值应为 (MACOS):
the values should be (MACOS):