go build build 是什么意思? (去构建与去安装)
What does go build build? (go build vs. go install)
新的 Go 程序员通常不知道或混淆基本的 go build 命令的作用。
go build
和 go install
命令究竟做了什么构建以及它们将 result/output 放在哪里?
go
命令的作用取决于我们 运行 它是用于 "normal" 包还是特殊的 "main"
包。
包裹
go build
构建您的包,然后 丢弃结果。
go install
构建然后 安装 包在您的 $GOPATH/pkg
目录中。
对于命令(包main
)
go build
构建命令并将结果留在 当前工作目录 .
go install
在临时目录中构建命令,然后将其移动到 $GOPATH/bin
。
要传递给 go build
什么?
您可以将 packages 传递给 go build
,您想要构建的包。您还可以从单个目录传递 .go
个文件的列表,然后将其视为指定单个包的源文件列表。
如果未提供任何包(导入路径),构建将应用于当前目录。
导入路径可能包含一个或多个 "..."
通配符(在这种情况下它是 模式 )。 ...
可以匹配任何字符串,例如net/...
匹配 net
包及其任何子文件夹中的包。命令
go build ./...
常用于构建当前文件夹中的包和递归向下的所有包。在项目根目录中发出的这个命令构建了完整的项目。
有关指定包的更多信息,运行 go help packages
。
关于模块
Go 1.11 引入了对 Go 模块的初步支持,从 Go 1.13 开始,模块成为默认支持。当 go
工具来自包含 go.mod
文件(或当前文件夹的父文件夹之一)的文件夹 运行 时,go
工具 运行 s 处于 module-aware 模式(旧模式称为 GOPATH 模式)。
In module-aware mode, GOPATH no longer defines the meaning of imports
during a build, but it still stores downloaded dependencies (in GOPATH/pkg/mod)
and installed commands (in GOPATH/bin, unless GOBIN is set).
构建模块时,构建的内容由构建列表指定。构建列表最初只包含主模块(包含go
命令所在目录的模块为运行),将主模块的依赖添加到构建列表中,递归(dependencies of dependencies也添加了)。
有关详细信息,运行 go help modules
。
基本上,您可以使用 go build
检查是否可以构建包(及其依赖项),而 go install
也(永久)将结果安装在 $GOPATH
。
如果一切正常,go build
将静默终止,如果包不能 built/compiled.
,则会给出错误消息
每当 go
工具安装包或二进制文件时,它还会安装它所具有的任何依赖项,因此 运行ning go install
还将安装您的程序所依赖的包(公开可用, "go gettable" 包), 自动.
首先,请阅读官方 How to Write Go Code 页面。
有关 go
工具的更多信息:Command go
您还可以通过运行以下命令获得更多帮助:
go help build
还值得注意的是,从 Go 1.5 go install
开始还删除了由 go build
(source):
创建的可执行文件
If 'go install' (with no arguments, meaning the current directory)
succeeds, remove the executable written by 'go build', if present. This avoids leaving a stale binary behind...
为了完成列表,go run
将您的应用程序编译到一个临时文件夹中,并启动该可执行二进制文件。当应用程序退出时,它会正确清理临时文件。
受戴夫·切尼 What does go build build?
启发的问题
对于包裹:
go build
: builds your package then discards the results
在 Go 1.10(2018 年第 1 季度)之后,这将不是真的,感谢 CL 68116 and CL 75473. See this thread,我在这里引用。
What do exactly the go build
and go install
commands build
每当 go 工具安装包或二进制文件时,它还会安装它所具有的任何依赖项,因此 运行 go install 还将安装您的程序所依赖的包(公开可用,"go gettable" 包) , 自动.
实际上...go install
也会随着 Go 1.10 的变化而变化,另外 新的缓存:
The "go install
" command no longer installs dependencies of the named packages (CL 75850).
If you run "go install foo
", the only thing installed is foo
.
Before, it varied. If dependencies were out-of-date, "go install
" also installed any dependencies.
The implicit installation of dependencies during "go install
" caused a lot of confusion and headaches for users, but it was previously necessary to enable incremental builds.
Not anymore.
We think that the new "install what I said
" semantics will be much more understandable, especially since it's clear from bug reports that many users already expected them.
To force installation of dependencies during "go install
", use the new "go install -i
", by analogy with "go build -i
" and "go test -i
".
The fact that "go install
" used to install any rebuilt dependencies caused confusion most often in conjunction with -a
, which means "force rebuild of all dependencies
".
Now, "go install -a myprog
" will force a complete rebuild of all dependencies of myprog
, as well as myprog
itself, but only myprog
will get installed. (All the rebuilt dependencies will still be saved in the build cache, of course.)
Making this case work more understandably is especially important in conjunction with the new content-based staleness analysis, because it sees good reasons to rebuild dependencies more often than before, which would have increased the amount of "why did my dependencies get installed" confusion.
For example, if you run "go install -gcflags=-N myprog
", that installs a myprog
built with no compiler optimizations, but it no longer also reinstalls the packages myprog
uses from the standard library without compiler optimizations.
新的 Go 程序员通常不知道或混淆基本的 go build 命令的作用。
go build
和 go install
命令究竟做了什么构建以及它们将 result/output 放在哪里?
go
命令的作用取决于我们 运行 它是用于 "normal" 包还是特殊的 "main"
包。
包裹
go build
构建您的包,然后 丢弃结果。go install
构建然后 安装 包在您的$GOPATH/pkg
目录中。
对于命令(包main
)
go build
构建命令并将结果留在 当前工作目录 .go install
在临时目录中构建命令,然后将其移动到$GOPATH/bin
。
要传递给 go build
什么?
您可以将 packages 传递给 go build
,您想要构建的包。您还可以从单个目录传递 .go
个文件的列表,然后将其视为指定单个包的源文件列表。
如果未提供任何包(导入路径),构建将应用于当前目录。
导入路径可能包含一个或多个 "..."
通配符(在这种情况下它是 模式 )。 ...
可以匹配任何字符串,例如net/...
匹配 net
包及其任何子文件夹中的包。命令
go build ./...
常用于构建当前文件夹中的包和递归向下的所有包。在项目根目录中发出的这个命令构建了完整的项目。
有关指定包的更多信息,运行 go help packages
。
关于模块
Go 1.11 引入了对 Go 模块的初步支持,从 Go 1.13 开始,模块成为默认支持。当 go
工具来自包含 go.mod
文件(或当前文件夹的父文件夹之一)的文件夹 运行 时,go
工具 运行 s 处于 module-aware 模式(旧模式称为 GOPATH 模式)。
In module-aware mode, GOPATH no longer defines the meaning of imports during a build, but it still stores downloaded dependencies (in GOPATH/pkg/mod) and installed commands (in GOPATH/bin, unless GOBIN is set).
构建模块时,构建的内容由构建列表指定。构建列表最初只包含主模块(包含go
命令所在目录的模块为运行),将主模块的依赖添加到构建列表中,递归(dependencies of dependencies也添加了)。
有关详细信息,运行 go help modules
。
基本上,您可以使用 go build
检查是否可以构建包(及其依赖项),而 go install
也(永久)将结果安装在 $GOPATH
。
go build
将静默终止,如果包不能 built/compiled.
每当 go
工具安装包或二进制文件时,它还会安装它所具有的任何依赖项,因此 运行ning go install
还将安装您的程序所依赖的包(公开可用, "go gettable" 包), 自动.
首先,请阅读官方 How to Write Go Code 页面。
有关 go
工具的更多信息:Command go
您还可以通过运行以下命令获得更多帮助:
go help build
还值得注意的是,从 Go 1.5 go install
开始还删除了由 go build
(source):
If 'go install' (with no arguments, meaning the current directory) succeeds, remove the executable written by 'go build', if present. This avoids leaving a stale binary behind...
为了完成列表,go run
将您的应用程序编译到一个临时文件夹中,并启动该可执行二进制文件。当应用程序退出时,它会正确清理临时文件。
受戴夫·切尼 What does go build build?
启发的问题对于包裹:
go build
: builds your package then discards the results
在 Go 1.10(2018 年第 1 季度)之后,这将不是真的,感谢 CL 68116 and CL 75473. See this thread,我在这里引用。
What do exactly the
go build
andgo install
commands build每当 go 工具安装包或二进制文件时,它还会安装它所具有的任何依赖项,因此 运行 go install 还将安装您的程序所依赖的包(公开可用,"go gettable" 包) , 自动.
实际上...go install
也会随着 Go 1.10 的变化而变化,另外 新的缓存:
The "
go install
" command no longer installs dependencies of the named packages (CL 75850).If you run "
go install foo
", the only thing installed isfoo
.Before, it varied. If dependencies were out-of-date, "
go install
" also installed any dependencies.
The implicit installation of dependencies during "go install
" caused a lot of confusion and headaches for users, but it was previously necessary to enable incremental builds.
Not anymore.
We think that the new "install what I said
" semantics will be much more understandable, especially since it's clear from bug reports that many users already expected them.
To force installation of dependencies during "go install
", use the new "go install -i
", by analogy with "go build -i
" and "go test -i
".The fact that "
go install
" used to install any rebuilt dependencies caused confusion most often in conjunction with-a
, which means "force rebuild of all dependencies
".
Now, "go install -a myprog
" will force a complete rebuild of all dependencies ofmyprog
, as well asmyprog
itself, but onlymyprog
will get installed. (All the rebuilt dependencies will still be saved in the build cache, of course.)
Making this case work more understandably is especially important in conjunction with the new content-based staleness analysis, because it sees good reasons to rebuild dependencies more often than before, which would have increased the amount of "why did my dependencies get installed" confusion.
For example, if you run "go install -gcflags=-N myprog
", that installs amyprog
built with no compiler optimizations, but it no longer also reinstalls the packagesmyprog
uses from the standard library without compiler optimizations.