什么是构建构建?(去构建与去安装) [英] What does go build build? (go build vs. go install)
问题描述
新的 Go 程序员通常不知道或混淆基本的 go build 命令的作用.
New Go programmers often don't know or get confused what the fundamental go build command does.
go build
和 go install
命令究竟做了什么构建,它们把结果/输出放在哪里?
What do exactly the go build
and go install
commands build and where do they put the result/output?
推荐答案
go
命令的作用取决于我们是为普通"包还是为特殊的 main"运行它"
包.
What the go
command does depends on whether we run it for a "normal" package or for the special "main"
package.
对于包裹
go build
构建您的包,然后丢弃结果.go install
在$GOPATH/pkg
目录中构建然后安装包.
go build
builds your package then discards the results.go install
builds then installs the package in your$GOPATH/pkg
directory.
对于命令(包main
)
go build
构建命令并将结果留在当前工作目录中.go install
在临时目录中构建命令,然后将其移动到$GOPATH/bin
.
go build
builds the command and leaves the result in the current working directory.go install
builds the command in a temporary directory then moves it to$GOPATH/bin
.
你可以将packages传递给go build
,你想要构建的包.您还可以从单个目录传递 .go
文件列表,然后将其视为指定单个包的源文件列表.
You may pass packages to go build
, packages you want to build. You may also pass a list of .go
files from a single directory, which is then treated as the list of source files specifying a single package.
如果未提供包(导入路径),则构建将应用于当前目录.
If no packages (import paths) are provided, the build is applied on the current directory.
一个导入路径可能包含一个或多个"..."
通配符(在这种情况下它是一个模式)....
可以匹配任何字符串,例如net/...
匹配 net
包及其任何子文件夹中的包.命令
An import path may contain one or more "..."
wildcards (in which case it is a pattern). ...
can match any string, e.g. net/...
matches the net
package and packages being in any of its subfolders. The command
go build ./...
通常用于在当前文件夹中构建包以及向下递归所有包.在项目根目录中发出的此命令将构建完整的项目.
often used to build the package in the current folder and all packages recursing down. This command issued in a project root builds the complete project.
有关指定包的更多信息,请运行 go help packages
.
For more about specifying packages, run go help packages
.
Go 1.11 中引入了对 Go 模块的初步支持,从 Go 1.13 开始,模块成为默认值.当 go
工具从包含 go.mod
文件(或当前文件夹的父文件夹之一)的文件夹中运行时,go
工具以模块感知模式运行(传统模式称为GOPATH模式).
Preliminary support for Go modules was introduced in Go 1.11, and modules became default starting with Go 1.13. When the go
tool is run from a folder which contains a go.mod
file (or one of the parents of the current folder), the go
tool runs in module-aware mode (the legacy mode is called GOPATH mode).
在module-aware模式下,GOPATH不再定义imports的含义在构建期间,但它仍然存储下载的依赖项(在 GOPATH/pkg/mod 中)和已安装的命令(在 GOPATH/bin 中,除非设置了 GOBIN).
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
命令的目录),并且将主模块的依赖项递归添加到构建列表中(依赖项的依赖项是还添加了).
When building modules, what is built is specified by the build list. The build list initially contains only the main module (the module containing the directory where the go
command is run), and the dependencies of the main module are added to the build list, recursively (dependencies of dependencies are also added).
有关详细信息,请运行 go help modules
.
For more info, run go help modules
.
基本上,您可以使用 go build
作为检查包是否可以构建(连同它们的依赖项),而 go install
还(永久)将结果安装在$GOPATH
的正确文件夹.
Basically you can use go build
as a check that the packages can be built (along with their dependencies) while go install
also (permanently) installs the results in the proper folders of your $GOPATH
.
go build
将无声地终止,如果包无法构建/编译,则会向您显示错误消息.
go build
will silently terminate if everything is OK, and will give you error messages if the packages cannot be built/compiled.
每当 go
工具安装包或二进制文件时,它也会安装它所具有的任何依赖项,因此运行 go install
还将安装程序依赖的包(公开可用), "go gettable" 包),自动.
Whenever the go
tool installs a package or binary, it also installs whatever dependencies it has, so running go install
will also install packages your program depends on (publicly available, "go gettable" packages), automatically.
首先,阅读官方如何编写 Go 代码 页面.
For a start, read the official How to Write Go Code page.
有关 go
工具的更多信息:Command go
More information about the go
tool: Command go
您还可以通过运行以下命令获得更多帮助:
You can also get more help by running the following command:
go help build
还值得注意的是,从 Go 1.5 开始,go install
还删除了由 go build
创建的可执行文件(来源):
It is also worth noting that starting with Go 1.5 go install
also removes executables created by go build
(source):
如果'go install'(不带参数,表示当前目录)成功,删除由 'go build' 编写的可执行文件(如果存在).这避免了留下陈旧的二进制文件......
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
将您的应用程序编译到一个临时文件夹中,并启动该可执行二进制文件.当应用退出时,它会正确清理临时文件.
To complete the list, go run
compiles your application into a temporary folder, and starts that executable binary. When the app exits, it properly cleans up the temporary files.
问题的灵感来自 Dave Cheney 的 什么是去构建构建?
这篇关于什么是构建构建?(去构建与去安装)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!