什么去构建构建? (进行构建与进行安装) [英] What does go build build? (go build vs. go install)

查看:126
本文介绍了什么去构建构建? (进行构建与进行安装)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

新的Go程序员经常不了解或困惑基本的go build命令的作用。

New Go programmers often don't know or get confused what the fundamental go build command does.

go build的确切作用是什么进行安装命令生成,它们将结果/输出放在哪里?

What do exactly the go build and go install commands build and where do they put the result/output?

推荐答案

go 命令的作用取决于我们是否为正常程序包或特殊的运行它主要 软件包。

What the go command does depends on whether we run it for a "normal" package or for the special "main" package.

对于软件包


  • 进行构建构建您的软件包,然后丢弃结果

  • 开始安装,然后在 $ GOPATH / pkg 目录中安装该软件包。

  • go build   builds your package then discards the results.
  • go install builds then installs the package in your $GOPATH/pkg directory.

对于命令(包 main

For commands (package main)


  • 进行构建将构建命令并将结果保留在当前工作目录。

  • 进行安装可以在tempora中构建命令ry目录然后将其移动到 $ 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.

导入路径可能包含一个或多个 ... 通配符(在这种情况下,它是 pattern )。 ... 可以匹配任何字符串,例如 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帮助软件包

For more about specifying packages, run go help packages.

Go 1.11中引入了对Go模块的初步支持,并且模块从2011年开始成为默认模块转到1.13。当从包含 go.mod 文件(或其中一个的父目录)的文件夹中运行 go 工具时当前文件夹), 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).


在模块感知模式下,GOPATH不再定义在构建期间导入
的含义,但仍存储下载的依赖项(在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帮助模块

For more info, run go help modules.

基本上,您可以使用 go build 来检查软件包是否可以内置(及其依赖项),同时去安装也会(永久地)将结果安装在 $ 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.

开始构建如果一切正常,将自动终止,如果无法构建软件包,则会显示错误消息/ compiled。

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 工具的详细信息:> 执行命令

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

要完成列表,请运行 将应用程序编译到一个临时文件夹中,然后启动该可执行二进制文件。应用退出后,它将正确清理临时文件。

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆