本文共 12859 字,大约阅读时间需要 42 分钟。
So far in our , you have used the command to automatically compile your source code and run the resulting executable. Although this command is useful for testing your code on the command line, distributing or deploying your application requires you to build your code into a shareable binary executable, or a single file containing machine byte code that can run your application. To do this, you can use the Go toolchain to build and install your program.
到目前为止,在“ 编写 ,您已经使用命令自动编译源代码并运行生成的可执行文件。 尽管此命令对于在命令行上测试代码很有用,但是分发或部署应用程序要求您将代码构建为可共享的二进制可执行文件或包含可运行应用程序的机器字节码的单个文件。 为此,您可以使用Go工具链来构建和安装程序。
In Go, the process of translating source code into a binary executable is called building. Once this executable is built, it will contain not only your application, but also all the support code needed to execute the binary on the target platform. This means that a Go binary does not need system dependencies such as Go tooling to run on a new system, unlike other languages like , , or . Putting these executables in an executable filepath on your own system will allow you to run the program from anywhere on your system. This is called installing the program onto your system.
在Go中,将源代码转换为二进制可执行文件的过程称为building 。 构建此可执行文件后,它将不仅包含您的应用程序,还包含在目标平台上执行二进制文件所需的所有支持代码。 这意味着Go二进制文件不需要诸如Go工具之类的系统依赖性就可以在新系统上运行,这与其他语言(如 , 或 。 将这些可执行文件放在您自己的系统上的可执行文件路径中,将使您可以在系统上的任何位置运行该程序。 这称为将程序安装到系统上。
In this tutorial, you will use the Go toolchain to run, build, and install a sample Hello, World!
program, allowing you to use, distribute, and deploy future applications effectively.
在本教程中,您将使用Go工具链来运行,构建和安装示例Hello, World!
程序,使您可以有效地使用,分发和部署将来的应用程序。
To follow the example in this article, you will need:
要遵循本文中的示例,您将需要:
A Go workspace set up by following .
通过遵循 Go工作区。
First, create an application to use as an example for demonstrating the Go toolchain. To do this, you will use the classic “Hello, World!” program from the tutorial.
首先,创建一个应用程序作为演示Go工具链的示例。 为此,您将使用经典的“ Hello,World!”。 教程中的程序。
Create a directory called greeter
in your src
directory:
在src
目录中创建一个名为greeter
的目录:
Next, move into the newly created directory and create the main.go
file in the text editor of your choice:
接下来,进入新创建的目录,并在您选择的文本编辑器中创建main.go
文件:
Once the file is open, add the following contents:
打开文件后,添加以下内容:
package mainimport "fmt"func main() { fmt.Println("Hello, World!")}
When run, this program will print the phrase Hello, World!
to the console, and then the program will exit successfully.
运行时,此程序将打印短语Hello, World!
到控制台,然后程序将成功退出。
Save and exit the file.
保存并退出文件。
To test the program, use the go run
command, as you’ve done in previous tutorials:
要测试程序,请使用go run
命令,就像您在之前的教程中所做的那样:
You’ll receive the following output:
您将收到以下输出:
Output Hello, World!
As mentioned before, the go run
command built your source file into an executable binary, and then ran the compiled program. However, this tutorial aims to build the binary in such a way that you can share and distribute it at will. To do this, you will use the go build
command in the next step.
如前所述, go run
命令将源文件构建为可执行二进制文件,然后运行编译的程序。 但是,本教程旨在以一种可以随意共享和分发的方式构建二进制文件。 为此,您将在下一步中使用go build
命令。
go build
构建Go二进制文件 (Building Go Binaries With go build
)Using go build
, you can generate an executable binary for our sample Go application, allowing you to distribute and deploy the program where you want.
使用go build
,可以为我们的示例Go应用程序生成可执行二进制文件,从而使您可以在所需的位置分发和部署程序。
Try this with main.go
. In your greeter
directory, run the following command:
试试main.go
在您的greeter
目录中,运行以下命令:
If you do not provide an argument to this command, go build
will automatically compile the main.go
program in your current directory. The command will include all your *.go
files in the directory. It will also build all of the supporting code needed to be able to execute the binary on any computer with the same system architecture, regardless of whether that system has the .go
source files, or even a Go installation.
如果您未为该命令提供参数,则go build
将自动在当前目录中编译main.go
程序。 该命令将在目录中包含所有*.go
文件。 它还将构建所有必需的支持代码,以便能够在具有相同系统架构的任何计算机上执行二进制文件,而不管该系统是否具有.go
源文件甚至Go安装。
In this case, you built your greeter
application into an executable file that was added to your current directory. Check this by running the ls
command:
在这种情况下,您将greeter
应用程序构建到已添加到当前目录的可执行文件中。 通过运行ls
命令进行检查:
If you are running macOS or Linux, you will find a new executable file that has been named after the directory in which you built your program:
如果您运行的是macOS或Linux,则会找到一个新的可执行文件,该文件已在生成程序的目录下命名:
Output greeter main.go
Note: On Windows, your executable will be greeter.exe
.
注意:在Windows上,您的可执行文件将是greeter.exe
。
By default go build
will generate an executable for the current . For example, if built on a linux/386
system, the executable will be compatible with any other linux/386
system, even if Go is not installed. Go supports building for other platforms and architectures, which you can read more about in our article.
默认情况下, go build
将为当前生成可执行文件。 例如,如果构建在linux/386
系统上,那么即使未安装Go,该可执行文件也将与任何其他linux/386
系统兼容。 Go支持针对其他平台和体系结构的构建,您可以在我们的文章中了解更多信息。
Now, that you’ve created your executable, run it to make sure the binary has been built correctly. On macOS or Linux, run the following command:
现在,您已经创建了可执行文件,运行它以确保二进制文件已正确构建。 在macOS或Linux上,运行以下命令:
On Windows, run:
在Windows上,运行:
The output of the binary will match the output from when you ran the program with go run
:
二进制文件的输出将与使用go run
程序时的输出匹配:
Output Hello, World!
Now you have created a single executable binary that contains, not only your program, but also all of the system code needed to run that binary. You can now distribute this program to new systems or deploy it to a server, knowing that the file will always run the same program.
现在,您已经创建了一个可执行二进制文件,其中不仅包含您的程序,还包含运行该二进制文件所需的所有系统代码。 现在,您知道该文件将始终运行同一程序,则可以将该程序分发到新系统或将其部署到服务器。
In the next section, this tutorial will explain how a binary is named and how you can change it, so that you can have better control over the build process of your program.
在下一节中,本教程将说明如何命名二进制文件以及如何对其进行更改,以便可以更好地控制程序的构建过程。
Now that you know how to generate an executable, the next step is to identify how Go chooses a name for the binary and to customize this name for your project.
现在您知道了如何生成可执行文件,下一步是确定Go如何为二进制文件选择一个名称并为您的项目自定义该名称。
When you run go build
, the default is for Go to automatically decide on the name of the generated executable. It does this in one of two ways: If you are using , then Go will use the last part of your module’s name; otherwise, Go will use the current directory’s name. This is the method used in the last section, when you created the greeter
directory, changed into it, and then ran go build
.
运行go build
,Go的默认设置是自动确定生成的可执行文件的名称。 它以两种方式之一执行此操作:如果正在使用 ,则Go将使用模块名称的最后一部分; 否则,Go将使用当前目录的名称。 这是上一节中使用的方法,当您创建greeter
目录时,将其更改greeter
目录,然后运行go build
。
Let’s take a closer look at the module method. If you had a go.mod
file in your project with a module
declaration such as the following:
让我们仔细看一下模块方法。 如果您的项目中有一个go.mod
文件,其中包含如下module
声明:
module github.com/sammy/shark
Then the default name for the generated executable would be shark
.
然后,生成的可执行文件的默认名称将为shark
。
In more complex programs that require specific naming conventions, these default values will not always be the best choice for naming your binary. In these cases, it would be best to customize your output with the -o
flag.
在需要特定命名约定的更复杂的程序中,这些默认值并非始终是命名二进制文件的最佳选择。 在这些情况下,最好使用-o
标志来自定义输出。
To test this out, change the name of the executable you made in the last section to hello
and have it placed in a sub-folder called bin
. You don’t have to create this folder; Go will do that on its own during the build process.
要进行测试,请将上一节中创建的可执行文件的名称更改为hello
并将其放置在名为bin
的子文件夹中。 您不必创建此文件夹; Go将在构建过程中自行完成。
Run the following go build
command with the -o
flag:
使用-o
标志运行以下go build
命令:
The -o
flag makes Go match the output of the command to whatever argument you chose. In this case, the result is a new executable named hello
in a sub-folder named bin
.
-o
标志使Go将命令的输出与您选择的任何参数匹配。 在这种情况下,结果是在名为bin
的子文件夹中的名为hello
的新可执行文件。
To test the new executable, change into the new directory and run the binary:
要测试新的可执行文件,请切换到新目录并运行二进制文件:
You will receive the following output:
您将收到以下输出:
Output Hello, World!
You can now customize the name of your executable to fit the needs of your project, completing our survey of how to build binaries in Go. But with go build
, you are still limited to running your binary from the current directory. In order to use newly built executables from anywhere on your system, you can install it using go install
.
现在,您可以自定义可执行文件的名称以适合您的项目需求,从而完成我们对如何在Go中构建二进制文件的调查。 但是使用go build
,您仍然仅限于从当前目录运行二进制文件。 为了从系统上的任何地方使用新建的可执行文件,您可以使用go install
。
go install
安装Go程序 (Installing Go Programs with go install
)So far in this article, we have discussed how to generate executable binaries from our .go
source files. These executables are helpful to distribute, deploy, and test, but they cannot yet be executed from outside of their source directories. This would be a problem if you wanted to actively use your program, such as if you developed a command line tool to help your workflow on your own system. To make the programs easier to use, you can install them into your system and access them from anywhere.
到目前为止,在本文中,我们已经讨论了如何从.go
源文件生成可执行二进制文件。 这些可执行文件对分发,部署和测试很有帮助,但它们仍无法从其源目录外部执行。 如果您想积极地使用程序,例如开发了命令行工具来帮助您的系统上的工作流程,那么这将是一个问题。 为了使程序更易于使用,您可以将其安装到系统中并从任何地方访问它们。
To understand what is meant by this, you will use the go install
command to install your sample application.
要了解其含义,您将使用go install
命令安装示例应用程序。
The go install
command behaves almost identically to go build
, but instead of leaving the executable in the current directory, or a directory specified by the -o
flag, it places the executable into the $GOPATH/bin
directory.
go install
命令的行为几乎与go build
相同,但不是将可执行文件保留在当前目录或-o
标志指定的目录中,而是将可执行文件放置在$GOPATH/bin
目录中。
To find where your $GOPATH
directory is located, run the following command:
要查找$GOPATH
目录的位置,请运行以下命令:
The output you receive will vary, but the default is the go
directory inside of your $HOME
directory:
您收到的输出将有所不同,但是默认值为$HOME
目录内的go
目录:
Output $HOME/go
Since go install
will place generated executables into a sub-directory of $GOPATH
named bin
, this directory must be added to the $PATH
environment variable. This is covered in the Creating Your Go Workspace step of the prerequisite article .
由于go install
会将生成的可执行文件放入$GOPATH
名为bin
的子目录中,因此必须将此目录添加到$PATH
环境变量中。 前提条件文章“ 的“ 创建Go工作区”步骤中对此进行了介绍。
With the $GOPATH/bin
directory set up, move back to your greeter
directory:
设置好$GOPATH/bin
目录后,返回到greeter
目录:
Now run the install command:
现在运行安装命令:
This will build your binary and place it in $GOPATH/bin
. To test this, run the following:
这将构建您的二进制文件并将其放置在$GOPATH/bin
。 要对此进行测试,请运行以下命令:
This will list the contents of $GOPATH/bin
:
这将列出$GOPATH/bin
的内容:
Output greeter
Note: The go install
command does not support the -o
flag, so it will use one of the default names described earlier to name the executable.
注: go install
命令不支持-o
标志,所以它会使用默认的名称之一前面描述来命名的可执行文件。
With the binary installed, test to see if the program will run from outside its source directory. Move back to your home directory:
安装二进制文件后,进行测试以查看该程序是否将从其源目录之外运行。 返回到您的主目录:
Use the following to run the program:
使用以下命令运行程序:
This will yield the following:
这将产生以下结果:
Output Hello, World!
Now you can take the programs you write and install them into your system, allowing you to use them from wherever, whenever you need them.
现在,您可以使用编写的程序并将其安装到系统中,从而可以随时随地使用它们。
In this tutorial, you demonstrated how the Go toolchain makes it easy to build executable binaries from source code. These binaries can be distributed to run on other systems, even ones that do not have Go tooling and environments. You also used go install
to automatically build and install our programs as executables in the system’s $PATH
. With go build
and go install
, you now have the ability to share and use your application at will.
在本教程中,您演示了Go工具链如何轻松地从源代码构建可执行二进制文件。 这些二进制文件可以分发以在其他系统上运行,即使是没有Go工具和环境的系统。 您还使用go install
来自动构建和安装程序,作为系统$PATH
中的可执行文件。 使用go build
和go install
,您现在可以随意共享和使用您的应用程序。
Now that you know the basics of go build
, you can explore how to make modular source code with the tutorial, or how to build for different platforms with . If you’d like to learn more about the Go programming language in general, check out the entire .
既然您已经了解了go build
的基础知识,那么您就可以探索如何使用的教程来制作模块化源代码,或者如何使用针对不同平台进行 。 如果您想全面了解Go编程语言,请阅读完整的“ 在Go中编写 。
翻译自:
转载地址:http://gthgb.baihongyu.com/