web应用程序体系结构_为不同的操作系统和体系结构构建Go应用程序

news/2024/7/8 4:33:06

web应用程序体系结构

In software development, it is important to consider the operating system and underlying processor architecture that you would like to compile your binary for. Since it is often slow or impossible to run a binary on a different OS/architecture platform, it is a common practice to build your final binary for many different platforms to maximize your program’s audience. However, this can be difficult when the platform you are using for development is different from the platform you want to deploy your program to. In the past, for example, developing a program on Windows and deploying it to a Linux or a macOS machine would involve setting up build machines for each of the environments you wanted binaries for. You’d also need to keep your tooling in sync, in addition to other considerations that would add cost and make collaborative testing and distribution more difficult.

在软件开发中,重要的是要考虑要为其编译二进制文件的操作系统和基础处理器体系结构 。 由于在不同的OS /体系结构平台上运行二进制文件通常很慢或不可能,因此通常的做法是为许多不同的平台构建最终的二进制文件以最大化程序的受众。 但是,当用于开发的平台与要将程序部署到的平台不同时,这可能会很困难。 例如,在过去,要在Windows上开发程序并将其部署到Linux或macOS机器上,将涉及为要使用二进制文件的每种环境设置构建机器。 除了其他考虑因素之外,您还需要保持工具同步,这会增加成本并使协作测试和分发更加困难。

Go solves this problem by building support for multiple platforms directly into the go build tool, as well as the rest of the Go toolchain. By using environment variables and build tags, you can control which OS and architecture your final binary is built for, in addition to putting together a workflow that can quickly toggle the inclusion of platform-dependent code without changing your codebase.

Go通过直接在go build工具以及其余Go工具链中go build对多个平台的支持来解决此问题。 通过使用环境变量和构建标签 ,您可以控制最终二进制文件针对的操作系统和体系结构,此外,还可以建立一个工作流,该工作流可以快速切换包含平台的代码,而无需更改代码库。

In this tutorial, you will put together a sample application that joins strings together into a filepath, create and selectively include platform-dependent snippets, and build binaries for multiple operating systems and system architectures on your own system, showing you how to use this powerful capability of the Go programming language.

在本教程中,您将组合一个示例应用程序,该示例应用程序将字符串连接到一个文件路径中,创建并有选择地包含平台相关的代码片段,并为自己系统上的多个操作系统和系统架构构建二进制文件,向您展示如何使用此功能强大的功能。 Go编程语言的功能。

先决条件 (Prerequisites)

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

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

  • A Go workspace set up by following How To Install Go and Set Up a Local Programming Environment.

    通过遵循如何安装Go和设置本地编程环境来设置 Go工作区。

适用于GOOSGOARCH平台 (Possible Platforms for GOOS and GOARCH)

Before showing how to control the build process to build binaries for different platforms, let’s first inspect what kinds of platforms Go is capable of building for, and how Go references these platforms using the environment variables GOOS and GOARCH.

在展示如何控制构建过程以为不同平台构建二进制文件之前,让我们首先检查Go能够构建哪种平台,以及Go如何使用环境变量GOOSGOARCH引用这些平台。

The Go tooling has a command that can print a list of the possible platforms that Go can build on. This list can change with each new Go release, so the combinations discussed here might not be the same on another version of Go. At the time of writing this tutorial, the current Go release is 1.13.

Go工具具有一个命令,该命令可以打印出可以构建Go的可能平台的列表。 该列表可能会随Go的每个新发行版而变化,因此此处讨论的组合在Go的另一个版本上可能会有所不同。 在撰写本教程时,当前的Go版本是1.13

To find this list of possible platforms, run the following:

要查找此可能平台的列表,请运行以下命令:

  • go tool dist list

    进入工具列表

You will receive an output similar to the following:

您将收到类似于以下内容的输出:


   
Output
aix/ppc64 freebsd/amd64 linux/mipsle openbsd/386 android/386 freebsd/arm linux/ppc64 openbsd/amd64 android/amd64 illumos/amd64 linux/ppc64le openbsd/arm android/arm js/wasm linux/s390x openbsd/arm64 android/arm64 linux/386 nacl/386 plan9/386 darwin/386 linux/amd64 nacl/amd64p32 plan9/amd64 darwin/amd64 linux/arm nacl/arm plan9/arm darwin/arm linux/arm64 netbsd/386 solaris/amd64 darwin/arm64 linux/mips netbsd/amd64 windows/386 dragonfly/amd64 linux/mips64 netbsd/arm windows/amd64 freebsd/386 linux/mips64le netbsd/arm64 windows/arm

This output is a set of key-value pairs separated by a /. The first part of the combination, before the /, is the operating system. In Go, these operating systems are possible values for the environment variable GOOS, pronounced “goose”, which stands for Go Operating System. The second part, after the /, is the architecture. As before, these are all possible values for an environment variable: GOARCH. This is pronounced “gore-ch”, and stands for Go Architecture.

此输出是由/分隔的一组键值对。 组合的第一部分/之前是操作系统。 在Go中,这些操作系统是环境变量GOOS可能值,发音为“ goose”,代表Go操作系统/之后的第二部分是体系结构。 和以前一样,这些都是环境变量GOARCH所有可能值。 这被称为“ gore-ch”,代表Go Architecture

Let’s break down one of these combinations to understand what it means and how it works, using linux/386 as an example. The key-value pair starts with the GOOS, which in this example would be linux, referring to the Linux OS. The GOARCH here would be 386, which stands for the Intel 80386 microprocessor.

让我们以linux/386为例,分解这些组合之一,以了解其含义和工作方式。 键值对以GOOS (在本示例中为linux开头,指的是Linux OS 。 GOARCH386 ,代表Intel 80386微处理器 。

There are many platforms available with the go build command, but a majority of the time you’ll end up using linux , windows, or darwin as a value for GOOS. These cover the big three OS platforms: Linux, Windows, and macOS, which is based on the Darwin operating system and is thus called darwin. However, Go can also cover less mainstream platforms like nacl, which represents Google’s Native Client.

go build命令有许多可用的平台,但是大多数情况下,您最终会使用linuxwindowsdarwin作为GOOS的值。 这些涵盖了三个主要的OS平台: Linux , Windows和macOS ,它们基于Darwin操作系统 ,因此被称为darwin 。 不过,Go还可以覆盖不太主流的平台,例如代表Google的Native Client的 nacl

When you run a command like go build, Go uses the current platform’s GOOS and GOARCH to determine how to build the binary. To find out what combination your platform is, you can use the go env command and pass GOOS and GOARCH as arguments:

当您运行诸如go build类的命令时,Go将使用当前平台的GOOSGOARCH来确定如何构建二进制文件。 要了解您的平台是什么组合,可以使用go env命令并将GOOSGOARCH作为参数传递:

  • go env GOOS GOARCH

    转到环境GOOS GOARCH

In testing this example, we ran this command on macOS on a machine with an AMD64 architecture, so we will receive the following output:

在测试此示例时,我们在具有AMD64架构的计算机上的macOS上运行了此命令,因此我们将收到以下输出:


   
Output
darwin amd64

Here the output of the command tells us that our system has GOOS=darwin and GOARCH=amd64.

在这里,命令的输出告诉我们我们的系统具有GOOS=darwinGOARCH=amd64

You now know what the GOOS and GOARCH are in Go, as well as their possible values. Next, you will put together a program to use as an example of how to use these environment variables and build tags to build binaries for other platforms.

现在,您知道Go中的GOOSGOARCH以及它们可能的值。 接下来,您将组合一个程序,以作为如何使用这些环境变量和构建标签来构建其他平台二进制文件的示例。

filepath.Join()编写一个平台相关的程序 (Write a Platform-Dependent Program with filepath.Join())

Before you start building binaries for other platforms, let’s build an example program. A good sample for this purpose is the Join function in the path/filepath package in the Go standard library. This function takes a number of strings and returns one string that is joined together with the correct filepath separator.

在开始为其他平台构建二进制文件之前,让我们构建一个示例程序。 一个很好的示例是Go标准库中path/filepath包中的Join函数。 此函数采用多个字符串,并返回一个字符串,该字符串与正确的文件路径分隔符连接在一起。

This is a good example program because the operation of the program depends on which OS it is running on. On Windows, the path separator is a backslash, \, while Unix-based systems use a forward slash, /.

这是一个很好的示例程序,因为程序的操作取决于它在哪个OS上运行。 在Windows上,路径分隔符是反斜杠\ ,而基于Unix的系统使用正斜杠/

Let’s start with building an application that uses filepath.Join(), and later, you’ll write your own implementation of the Join() function that customizes the code to the platform-specific binaries.

让我们从构建使用filepath.Join()的应用程序开始,然后,您将编写自己的Join()函数实现,该函数将代码定制为特定于平台的二进制文件。

First, create a folder in your src directory with the name of your app:

首先,在src目录中使用应用程序名称创建一个文件夹:

  • mkdir app

    mkdir 应用

Move into that directory:

移至该目录:

  • cd app

    光盘应用

Next, create a new file in your text editor of choice named main.go. For this tutorial, we will use Nano:

接下来,在您选择的文本编辑器中创建一个名为main.go的新文件。 在本教程中,我们将使用Nano:

  • nano main.go

    纳米main.go

Once the file is open, add the following code:

打开文件后,添加以下代码:

src/app/main.go
src / app / main.go
package main

import (
  "fmt"
  "path/filepath"
)

func main() {
  s := filepath.Join("a", "b", "c")
  fmt.Println(s)
}

The main() function in this file uses filepath.Join() to concatenate three strings together with the correct, platform-dependent path separator.

此文件中的main()函数使用filepath.Join()将三个字符串与正确的,平台相关的路径分隔符连接在一起。

Save and exit the file, then run the program:

保存并退出文件,然后运行程序:

  • go run main.go

    去运行main.go

When running this program, you will receive different output depending on which platform you are using. On Windows, you will see the strings separated by \:

运行此程序时,根据使用的平台,您将收到不同的输出。 在Windows上,您会看到用\分隔的字符串:


   
Output
a\b\c

On Unix systems like macOS and Linux, you will receive the following:

在macOS和Linux等Unix系统上,您将收到以下信息:


   
Output
a/b/c

This shows that, because of the different filesystem protocols used on these operating systems, the program will have to build different code for the different platforms. But since it already uses a different file separator depending on the OS, we know that filepath.Join() already accounts for the difference in platform. This is because the Go tool chain automatically detects your machine’s GOOS and GOARCH and uses this information to use the code snippet with the right build tags and file separator.

这表明,由于这些操作系统上使用的文件系统协议不同,该程序将不得不为不同的平台构建不同的代码。 但是由于它已经根据操作系统使用了不同的文件分隔符,因此我们知道filepath.Join()已经解决了平台上的差异。 这是因为Go工具链会自动检测机器的GOOSGOARCH并使用此信息来使用带有正确构建标记和文件分隔符的代码段。

Let’s consider where the filepath.Join() function gets its separator from. Run the following command to inspect the relevant snippet from Go’s standard library:

让我们考虑一下filepath.Join()函数从何处获取其分隔符。 运行以下命令以检查Go的标准库中的相关代码段:

  • less /usr/local/go/src/os/path_unix.go

    少/usr/local/go/src/os/path_unix.go

This will display the contents of path_unix.go. Look for the following part of the file:

这将显示path_unix.go的内容。 查找文件的以下部分:

/usr/local/go/os/path_unix.go
/usr/local/go/os/path_unix.go
. . .
// +build aix darwin dragonfly freebsd js,wasm linux nacl netbsd openbsd solaris

package os

const (
  PathSeparator     = '/' // OS-specific path separator
  PathListSeparator = ':' // OS-specific path list separator
)
. . .

This section defines the PathSeparator for all of the varieties of Unix-like systems that Go supports. Notice all of the build tags at the top, which are each one of the possible Unix GOOS platforms associated with Unix. When the GOOS matches these terms, your program will yield the Unix-styled filepath separator.

本节为Go支持的所有各种Unix类系统定义PathSeparator 。 注意顶部的所有构建标记,它们是与Unix相关的每种可能的Unix GOOS平台之一。 当GOOS符合这些条件时,您的程序将产生Unix样式的文件路径分隔符。

Press q to return to the command line.

q返回到命令行。

Next, open the file that defines the behavior of filepath.Join() when used on Windows:

接下来,打开定义在Windows上使用时filepath.Join()的行为的文件:

  • less /usr/local/go/src/os/path_windows.go

    少/usr/local/go/src/os/path_windows.go

You will see the following:

您将看到以下内容:

/usr/local/go/src/os/path_windows.go
/usr/local/go/src/os/path_windows.go
. . .
package os

const (
        PathSeparator     = '\\' // OS-specific path separator
        PathListSeparator = ';'  // OS-specific path list separator
)
. . .

Although the value of PathSeparator is \\ here, the code will render the single backslash (\) needed for Windows filepaths, since the first backslash is only needed as an escape character.

尽管此处的PathSeparator值为\\ ,但是代码将呈现Windows文件路径所需的单个反斜杠( \ ),因为第一个反斜杠仅需要用作转义字符。

Notice that, unlike the Unix file, there are no build tags at the top. This is because GOOS and GOARCH can also be passed to go build by adding an underscore (_) and the environment variable value as a suffix to the filename, something we will go into more in the section Using GOOS and GOARCH File Name Suffixes. Here, the _windows part of path_windows.go makes the file act as if it had the build tag // +build windows at the top of the file. Because of this, when your program is run on Windows, it will use the constants of PathSeparator and PathListSeparator from the path_windows.go code snippet.

请注意,与Unix文件不同,顶部没有构建标记。 这是因为通过在文件名中添加下划线( _ )和环境变量值作为后缀,也可以传递GOOSGOARCH进行go build ,我们将在使用GOOS和GOARCH文件名后缀部分中介绍更多内容。 在这里, _windows的一部分path_windows.go使文件充当如果它有构建标签// +build windows在文件的顶部。 因此,当您的程序在Windows上运行时,它将使用path_windows.go代码段中的PathSeparatorPathListSeparator常量。

To return to the command line, quit less by pressing q.

要返回命令行,请按q less退出。

In this step, you built a program that showed how Go converts the GOOS and GOARCH automatically into build tags. With this in mind, you can now update your program and write your own implementation of filepath.Join(), using build tags to manually set the correct PathSeparator for Windows and Unix platforms.

在此步骤中,您构建了一个程序,该程序显示Go如何将GOOSGOARCH自动转换为构建标签。 考虑到这一点,您现在可以使用构建标签更新程序并编写自己的filepath.Join() ,使用构建标记为Windows和Unix平台手动设置正确的PathSeparator

实现特定于平台的功能 (Implementing a Platform-Specific Function)

Now that you know how Go’s standard library implements platform-specific code, you can use build tags to do this in your own app program. To do this, you will write your own implementation of filepath.Join().

既然您知道Go的标准库如何实现特定于平台的代码,就可以在您自己的app程序中使用构建标记来执行此操作。 为此,您将编写自己的filepath.Join()

Open up your main.go file:

打开您的main.go文件:

  • nano main.go

    纳米main.go

Replace the contents of main.go with the following, using your own function called Join():

使用您自己的名为Join()的函数,将main.go的内容替换为以下内容:

src/app/main.go
src / app / main.go
package main

import (
  "fmt"
  "strings"
)

func Join(parts ...string) string {
  return strings.Join(parts, PathSeparator)
}

func main() {
  s := Join("a", "b", "c")
  fmt.Println(s)
}

The Join function takes a number of parts and joins them together using the strings.Join() method from the strings package to concatenate the parts together using the PathSeparator.

Join函数接受多个parts ,并使用strings包中的strings.Join()方法将它们连接在一起,以使用PathSeparator将这些parts连接在一起。

You haven’t defined the PathSeparator yet, so do that now in another file. Save and quit main.go, open your favorite editor, and create a new file named path.go:

您尚未定义PathSeparator ,因此现在在另一个文件中进行定义。 保存并退出main.go ,打开您喜欢的编辑器,然后创建一个名为path.go的新文件:

nano path.go

Define the PathSeparator and set it equal to the Unix filepath separator, /:

定义PathSeparator并将其设置为等于Unix文件路径分隔符/

src/app/path.go
src / app / path.go
package main

const PathSeparator = "/"

Compile and run the application:

编译并运行该应用程序:

  • go build

    去建造
  • ./app

    ./app

You’ll receive the following output:

您将收到以下输出:


   
Output
a/b/c

This runs successfully to get a Unix-style filepath. But this isn’t yet what we want: the output is always a/b/c, regardless of what platform it runs on. To add in the functionality to create Windows-style filepaths, you will need to add a Windows version of the PathSeparator and tell the go build command which version to use. In the next section, you will use build tags to accomplish this.

这将成功运行以获取Unix样式的文件路径。 但这还不是我们想要的:输出始终为a/b/c ,无论它在什么平台上运行。 要添加创建Windows样式文件路径的功能,您需要添加Windows版本的PathSeparator并告诉go build命令使用哪个版本。 在下一节中,您将使用构建标签来完成此任务。

使用GOOSGOARCH构建标签 (Using GOOS or GOARCH Build Tags)

To account for Windows platforms, you will now create an alternate file to path.go and use build tags to make sure the code snippets only run when GOOS and GOARCH are the appropriate platform.

为了说明Windows平台,您现在path.go创建一个备用文件,并使用构建标记来确保仅在GOOSGOARCH是合适的平台时运行代码片段。

But first, add a build tag to path.go to tell it to build for everything except for Windows. Open up the file:

但首先,将一个构建标记添加到path.go以告诉它为Windows以外的所有内容进行构建。 打开文件:

  • nano path.go

    纳米路径

Add the following highlighted build tag to the file:

将以下突出显示的构建标记添加到文件中:

src/app/path.go
src / app / path.go
// +build !windows

package main

const PathSeparator = "/"

Go build tags allow for inverting, meaning that you can instruct Go to build this file for any platform except for Windows. To invert a build tag, place a ! before the tag.

Go构建标记允许反转,这意味着您可以指示Go为除Windows以外的任何平台构建此文件。 要反转构建标记,请放置! 在标签之前。

Save and exit the file.

保存并退出文件。

Now, if you were to run this program on Windows, you would get the following error:

现在,如果要在Windows上运行此程序,则会出现以下错误:


   
Output
./main.go:9:29: undefined: PathSeparator

In this case, Go would not be able to include path.go to define the variable PathSeparator.

在这种情况下,Go将无法包含path.go来定义变量PathSeparator

Now that you have ensured that path.go will not run when GOOS is Windows, add a new file, windows.go:

既然您已经确保当GOOS是Windows时path.go不会运行,请添加一个新文件windows.go

  • nano windows.go

    纳米windows.go

In windows.go, define the Windows PathSeparator, as well as a build tag to let the go build command know it is the Windows implementation:

windows.go ,定义Windows PathSeparator以及一个build标签,以使go build命令知道它是Windows实现:

src/app/windows.go
src / app / windows.go
// +build windows

package main

const PathSeparator = "\\"

Save the file and exit from the text editor. The application can now compile one way for Windows and another for all other platforms.

保存文件并退出文本编辑器。 现在,该应用程序可以针对Windows编译一种方式,而针对所有其他平台编译另一种方式。

While the binaries will now build correctly for their platforms, there are further changes you must make in order to compile for a platform that you do not have access to. To do this, you will alter your local GOOS and GOARCH environment variables in the next step.

虽然二进制文件现在将针对其平台正确构建,但是您必须进行进一步更改才能为您无法访问的平台进行编译。 为此,您将在下一步中更改本地GOOSGOARCH环境变量。

使用本地GOOSGOARCH环境变量 (Using Your Local GOOS and GOARCH Environment Variables)

Earlier, you ran the go env GOOS GOARCH command to find out what OS and architecture you were working on. When you ran the go env command, it looked for the two environment variables GOOS and GOARCH; if found, their values would be used, but if not found, then Go would set them with the information for the current platform. This means that you can change GOOS or GOARCH so that they do not default to your local OS and architecture.

之前,您运行了go env GOOS GOARCH命令来查找正在使用的操作系统和体系结构。 运行go env命令时,它将查找两个环境变量GOOSGOARCH ; 如果找到,则将使用它们的值,但如果找不到,则Go会将它们设置为当前平台的信息。 这意味着您可以更改GOOSGOARCH这样它们就不会默认为您的本地OS和体系结构。

The go build command behaves in a similar manner to the go env command. You can set either the GOOS or GOARCH environment variables to build for a different platform using go build.

go build命令的行为与go env命令类似。 您可以使用go build设置GOOSGOARCH环境变量以针对其他平台进行go build

If you are not using a Windows system, build a windows binary of app by setting the GOOS environment variable to windows when running the go build command:

如果你不使用Windows系统,建立一个windows的二进制app通过设置GOOS环境变量windows运行时go build命令:

  • GOOS=windows go build

    GOOS = Windows进行构建

Now list the files in your current directory:

现在列出当前目录中的文件:

  • ls

    ls

The output of listing the directory shows there is now an app.exe Windows executable in the project directory:

列出目录的输出显示项目目录中现在有一个app.exe Windows可执行文件:


   
Output
app app.exe main.go path.go windows.go

Using the file command, you can get more information about this file, confirming its build:

使用file命令,您可以获取有关此文件的更多信息,并确认其构建:

  • file app.exe

    文件app.exe

You will receive:

您将收到:


   
Output
app.exe: PE32+ executable (console) x86-64 (stripped to external PDB), for MS Windows

You can also set one, or both environment variables at build time. Run the following:

您还可以在构建时设置一个或两个环境变量。 运行以下命令:

  • GOOS=linux GOARCH=ppc64 go build

    GOOS = linux GOARCH = ppc64进行构建

Your app executable will now be replaced by a file for a different architecture. Run the file command on this binary:

您的app可执行文件现在将替换为其他体系结构的文件。 在此二进制file上运行file命令:

  • file app

    文件应用

You will receive output like the following:

您将收到类似以下的输出:

app: ELF 64-bit MSB executable, 64-bit PowerPC or cisco 7500, version 1 (SYSV), statically linked, not stripped

By setting your local GOOS and GOARCH environment variables, you can now build binaries for any of Go’s compatible platforms without a complicated configuration or setup. Next, you will use filename conventions to keep your files neatly organized and build for specific platforms automatically wihout build tags.

通过设置本地GOOSGOARCH环境变量,您现在可以为Go的任何兼容平台构建二进制文件,而无需进行复杂的配置或设置。 接下来,您将使用文件名约定来使您的文件井井有条,并针对特定平台进行自动构建标记。

使用GOOSGOARCH文件名后缀 (Using GOOS and GOARCH Filename Suffixes)

As you saw earlier, the Go standard library makes heavy use of build tags to simplify code by separating out different platform implementations into different files. When you opened the os/path_unix.go file, there was a build tag that listed all of the possible combinations that are considered Unix-like platforms. The os/path_windows.go file, however, contained no build tags, because the suffix on the filename sufficed to tell Go which platform the file was meant for.

如前所述,Go标准库通过将不同的平台实现分离到不同的文件中,大量使用了构建标记来简化代码。 当您打开os/path_unix.go文件时,有一个build标签列出了所有可能的组合,这些组合被视为类Unix平台。 但是, os/path_windows.go文件不包含构建标记,因为文件名上的后缀足以告诉Go文件所指向的平台。

Let’s look at the syntax of this feature. When naming a .go file, you can add GOOS and GOARCH as suffixes to the file’s name in that order, separating the values by underscores (_). If you had a Go file named filename.go, you could specify the OS and architecture by changing the filename to filename_GOOS_GOARCH.go. For example, if you wished to compile it for Windows with 64-bit ARM architecture, you would make the name of the file filename_windows_arm64.go. This naming convention helps keep code neatly organized.

让我们看一下此功能的语法。 命名.go文件时,您可以按顺序将GOOSGOARCH作为后缀添加到文件名,并用下划线( _ )分隔值。 如果您有一个名为filename.go的Go文件,则可以通过将文件filename_ GOOS _ GOARCH .go更改为filename_ GOOS _ GOARCH .go来指定操作系统和体系结构。 例如,如果您希望针对具有64位ARM架构的 Windows进行编译,则可以使用文件filename_windows_arm64.go 。 此命名约定有助于使代码井井有条。

Update your program to use the filename suffixes instead of build tags. First, rename the path.go and windows.go file to use the convention used in the os package:

更新您的程序以使用文件名后缀而不是构建标记。 首先,重命名path.gowindows.go文件以使用os包中使用的约定:

  • mv path.go path_unix.go

    mv path.go path_unix.go
  • mv windows.go path_windows.go

    mv windows.go path_windows.go

With the two filenames changed, you can remove the build tag you added to path_windows.go:

更改两个文件名后,您可以删除添加到path_windows.go的build标记:

  • nano path_windows.go

    纳米path_windows.go

Remove // +build windows so that your file looks like this:

删除// +build windows以使您的文件如下所示:

path_windows.go
path_windows.go
package main

const PathSeparator = "\\"

Save and exit from the file.

保存并退出文件。

Because unix is not a valid GOOS, the _unix.go suffix has no meaning to the Go compiler. It does, however, convey the intended purpose of the file. Like the os/path_unix.go file, your path_unix.go file still needs to use build tags, so keep that file unchanged.

因为unix不是有效的GOOS ,所以_unix.go后缀对Go编译器没有意义。 但是,它确实传达了文件的预期目的。 像os/path_unix.go文件一样,您的path_unix.go文件仍然需要使用构建标记,因此请保持该文件不变。

By using filename conventions, you removed uneeded build tags from your source code and made the filesystem cleaner and clearer.

通过使用文件名约定,您从源代码中删除了不需要的构建标记,并使文件系统更清晰。

结论 (Conclusion)

The ability to generate binaries for multiple platforms that require no dependencies is a powerful feature of the Go toolchain. In this tutorial, you used this capability by adding build tags and filename suffixes to mark certain code snippets to only compile for certain architectures. You created your own platorm-dependent program, then manipulated the GOOS and GOARCH environment variables to generate binaries for platforms beyond your current platform. This is a valuable skill, because it is a common practice to have a continuous integration process that automatically runs through these environment variables to build binaries for all platforms.

可以为不需要依赖关系的多个平台生成二进制文件,这是Go工具链的强大功能。 在本教程中,您通过添加构建标记和文件名后缀来标记某些代码片段以仅针对某些体系结构进行编译,从而使用了此功能。 您创建了自己的依赖于platorm的程序,然后操纵GOOSGOARCH环境变量来为当前平台之外的平台生成二进制文件。 这是一项宝贵的技能,因为通常的做法是具有一个持续集成过程,该过程自动运行这些环境变量来为所有平台构建二进制文件。

For further study on go build, check out our Customizing Go Binaries with Build Tags tutorial. 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中编写代码”系列 。

翻译自: https://www.digitalocean.com/community/tutorials/building-go-applications-for-different-operating-systems-and-architectures

web应用程序体系结构


http://www.niftyadmin.cn/n/3649685.html

相关文章

我的个人博客正式关联第三方博客平台

第三方博客平台简介 第三方博客指的是不要求自己有域名,空间,服务器,仅在大型门户网址注册就可运行的博客平台。 这类博客有新浪,搜狐,和讯,网易等。第三方博客现在已经成为更多网络爱好者发布自己心情&am…

[JavaME]手机申请移动分配的动态IP?(2)

[JavaME]手机申请移动分配的动态IP(2)?先用ServerSocketConnection.open然后向某服务器询问手机自己的IP? Hi,继续上回的讨论《[JavaME]手机是否能够申请到动态IP?》。上回说到申请动态IP的调试顺序可能反了,经过今天…

云计算到底算啥?李彦宏、马化腾PK马云

一千个人眼里,有一千个云计算。作为一个热门词汇,“云计算”到底是什么,依然给人云里雾里的印象。在日前举办的IT领袖峰会(深圳)上,业界大佬围绕云计算的争议,成为峰会当天最大的“看点”。以百度CEO李彦宏、腾讯董事会主席马化腾为代表的技术派对云计算大泼冷水,与以阿里巴巴主…

ionic移动开发常用的HTML组件总结

ionic常用的11个组件 header组件list组件卡片组件复选框组件单选框组件下拉选择组件按钮组件chip组件图形组件输入组件栅格化组件 1、header组件 演示代码 <ion-header ><ion-toolbar><ion-title>ionic组件header部分</ion-title><ion-button s…

[JavaME]手机是否能够申请到动态IP?

[JavaME]手机是否能够申请到动态IP?用ServerSocketConnection&#xff1f;http://www.j2medev.com/bbs/dispbbs.asp?boardid21&id4099&star1#2083 &#xff0c;这里是原始讨论贴。这里我们给出一个思路&#xff1a;可以先这样&#xff0c;手机先访问一个固定的网址&a…

给数学的孩子们推荐的十部经典电影~

0) 中文名称&#xff1a;死亡密码 英文名称&#xff1a;π 别 名&#xff1a;3.14159265358(USA) 发行时间&#xff1a;1998年07月10日 科幻惊栗手法描写一名天才数学家触目惊心的经历。才华盖世的数学家马斯在过去十年来&#xff0c;发现股票市场在混乱波动背后…

移动端开发——ionic集成toast消息提示插件

新建toast.service文件进行方法封装 1、新建文件命令 ionic g service service/toast2、toast.service完整代码 import { Injectable } from angular/core; import {ToastController} from "ionic/angular";Injectable({providedIn: root }) export class ToastSe…