如何构建和安装Go程序

介绍 (Introduction)

So far in our How To Code in Go series, you have used the command go run 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 run命令自动编译源代码并运行生成的可执行文件。 尽管此命令对于在命令行上测试代码很有用,但是分发或部署应用程序要求您将代码构建为可共享的二进制可执行文件或包含可运行应用程序的机器字节码的单个文件。 为此,您可以使用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 Ruby, Python, or Node.js. 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工具之类的系统依赖性就可以在新系统上运行,这与其他语言(如RubyPythonNode.js)不同 。 将这些可执行文件放在您自己的系统上的可执行文件路径中,将使您可以在系统上的任何位置运行该程序。 这称为程序安装到系统上。

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! 程序,使您可以有效地使用,分发和部署将来的应用程序。

先决条件 (Prerequisites)

To follow the example in this article, you will need:

要遵循本文中的示例,您将需要:

设置并运行Go Binary (Setting Up and Running the Go Binary)

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 How To Write Your First Program in Go tutorial.

首先,创建一个应用程序作为演示Go工具链的示例。 为此,您将使用经典的“ Hello,World!”。 如何编写Go中的第一个程序教程中的程序。

Create a directory called greeter in your src directory:

src目录中创建一个名为greeter的目录:

  • mkdir greeter

    mkdir greeter

Next, move into the newly created directory and create the main.go file in the text editor of your choice:

接下来,进入新创建的目录,并在您选择的文本编辑器中创建main.go文件:

  • cd greeter

    cd迎宾
  • nano main.go

    纳米main.go

Once the file is open, add the following contents:

打开文件后,添加以下内容:

src/greeter/main.go
src / greeter / main.go
package main

import "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命令,就像您在之前的教程中所做的那样:

  • go run main.go

    去运行main.go

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目录中,运行以下命令:

  • go build

    去建造

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命令进行检查:

  • ls

    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 platform and architecture. 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 Building Go Applications for Different Operating Systems and Architectures article.

默认情况下, go build将为当前平台和体系结构生成可执行文件。 例如,如果构建在linux/386系统上,那么即使未安装Go,该可执行文件也将与任何其他linux/386系统兼容。 Go支持针对其他平台和体系结构的构建,您可以在我们的针对不同操作系统和体系结构的构建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上,运行以下命令:

  • ./greeter

    。/招待员

On Windows, run:

在Windows上,运行:

  • greeter.exe

    问候程序

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.

在下一节中,本教程将说明如何命名二进制文件以及如何对其进行更改,以便可以更好地控制程序的构建过程。

更改二进制名称 (Changing the Binary Name)

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 Go Modules, 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 Modules ,则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声明:

go.mod
go.mod
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命令:

  • go build -o bin/hello

    去构建-o bin / hello

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:

要测试新的可执行文件,请切换到新目录并运行二进制文件:

  • cd bin

    光盘仓
  • ./hello

    。/你好

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目录的位置,请运行以下命令:

  • go env GOPATH

    转到环境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 How To Install Go and Set Up a Local Programming Environment.

由于go install会将生成的可执行文件放入$GOPATH名为bin的子目录中,因此必须将此目录添加到$PATH环境变量中。 前提条件文章“ 如何安装Go和设置本地编程环境”的“ 创建Go工作区”步骤中对此进行了介绍。

With the $GOPATH/bin directory set up, move back to your greeter directory:

设置好$GOPATH/bin目录后,返回到greeter目录:

  • cd ..

    光盘..

Now run the install command:

现在运行安装命令:

  • go install

    去安装

This will build your binary and place it in $GOPATH/bin. To test this, run the following:

这将构建您的二进制文件并将其放置在$GOPATH/bin 。 要对此进行测试,请运行以下命令:

  • ls $GOPATH/bin

    ls $ 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:

安装二进制文件后,进行测试以查看该程序是否将从其源目录之外运行。 返回到您的主目录:

  • cd $HOME

    cd $ HOME

Use the following to run the program:

使用以下命令运行程序:

  • greeter

    招待员

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.

现在,您可以使用编写的程序并将其安装到系统中,从而可以随时随地使用它们。

结论 (Conclusion)

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 buildgo install ,您现在可以随意共享和使用您的应用程序。

Now that you know the basics of go build, you can explore how to make modular source code with the Customizing Go Binaries with Build Tags tutorial, or how to build for different platforms with Building Go Applications for Different Operating Systems and Architectures. If you’d like to learn more about the Go programming language in general, check out the entire How To Code in Go series.

既然您已经了解了go build的基础知识,那么您就可以探索如何使用带有构建标签定制Go Binaries教程来制作模块化源代码,或者如何使用针对不同操作系统和体系结构构建Go应用程序来针对不同平台进行构建 。 如果您想全面了解Go编程语言,请阅读完整的“ 如何在Go中编写代码”系列

翻译自: https://www.digitalocean.com/community/tutorials/how-to-build-and-install-go-programs

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值