我可以根据要构建的OS导入Golang软件包吗? [英] Can I import a Golang package based on the OS I'm building for?

查看:121
本文介绍了我可以根据要构建的OS导入Golang软件包吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个go项目,该项目基于哪个OS,在某些情况下取决于哪个发行版,我想使用一个系统化的客户端包,一个Upstart客户端包,一个sysv客户端包,以及一个已启动的客户端包.是否可以有选择地导入每个软件包,以便仅导入要为其构建的每个OS/发行版所需的软件包?还是我必须为每个操作系统/发行版导入每个软件包?

Say I have a go project that based on which OS, and in some cases which distro, I want to use say a Systemd client package vs an Upstart client package vs a sysv client package vs a launchd client package. Is it possible to selectively import each package so I only import the one I need per OS/distro I'm building for? Or do I have to import each package for each OS/distro?

推荐答案

打包版本

构建约束

构建约束(也称为构建标签)是一行注释, 开始

A build constraint, also known as a build tag, is a line comment that begins

// +build

列出了将文件包含在文件中的条件 包裹.约束可能会出现在任何类型的源文件中(不仅仅是 转到),但它们必须出现在文件顶部附近,且仅在前面 空白行和其他行注释.这些规则意味着在Go文件中 构建约束必须出现在package子句之前.

that lists the conditions under which a file should be included in the package. Constraints may appear in any kind of source file (not just Go), but they must appear near the top of the file, preceded only by blank lines and other line comments. These rules mean that in Go files a build constraint must appear before the package clause.

要从软件包文档中区分构建约束,请进行一系列操作 的构建约束必须后跟空白行.

To distinguish build constraints from package documentation, a series of build constraints must be followed by a blank line.

将构建约束评估为以空格分隔的选项的OR; 每个选项的求值均以其逗号分隔的术语的AND表示;和 每个术语是一个字母数字单词,或者在其前面加!的否定. 也就是说,构建约束:

A build constraint is evaluated as the OR of space-separated options; each option evaluates as the AND of its comma-separated terms; and each term is an alphanumeric word or, preceded by !, its negation. That is, the build constraint:

// +build linux,386 darwin,!cgo

对应于布尔公式:

(linux AND 386) OR (darwin AND (NOT cgo))

文件可能具有多个构建约束.总体约束是 各个约束的AND.也就是说,构建约束:

A file may have multiple build constraints. The overall constraint is the AND of the individual constraints. That is, the build constraints:

// +build linux darwin
// +build 386

对应于布尔公式:

(linux OR darwin) AND 386

在特定的构建过程中,满足以下条件:

During a particular build, the following words are satisfied:

- the target operating system, as spelled by runtime.GOOS
- the target architecture, as spelled by runtime.GOARCH
- the compiler being used, either "gc" or "gccgo"
- "cgo", if ctxt.CgoEnabled is true
- "go1.1", from Go version 1.1 onward
- "go1.2", from Go version 1.2 onward
- "go1.3", from Go version 1.3 onward
- "go1.4", from Go version 1.4 onward
- "go1.5", from Go version 1.5 onward
- "go1.6", from Go version 1.6 onward
- any additional words listed in ctxt.BuildTags

如果是文件名,则在去除扩展名和可能的_test之后 后缀,匹配以下任何一种模式:

If a file's name, after stripping the extension and a possible _test suffix, matches any of the following patterns:

*_GOOS
*_GOARCH
*_GOOS_GOARCH

(例如:source_windows_amd64.go),其中GOOS和GOARCH代表任何 已知的操作系统和体系结构值,然后 文件被认为具有隐式构建约束,要求 这些术语(除了文件中的任何显式约束之外).

(example: source_windows_amd64.go) where GOOS and GOARCH represent any known operating system and architecture values respectively, then the file is considered to have an implicit build constraint requiring those terms (in addition to any explicit constraints in the file).

要避免考虑将文件用于构建:

To keep a file from being considered for the build:

// +build ignore

(其他任何不满意的词也可以,但是忽略"是 常规.)

(any other unsatisfied word will work as well, but "ignore" is conventional.)

仅在使用cgo且仅在Linux和OS X上才能生成文件:

To build a file only when using cgo, and only on Linux and OS X:

// +build linux,cgo darwin,cgo

此类文件通常与实现 其他系统的默认功能,在这种情况下, 带有约束:

Such a file is usually paired with another file implementing the default functionality for other systems, which in this case would carry the constraint:

// +build !linux,!darwin !cgo

命名文件dns_windows.go将导致仅在以下情况下将其包含 为Windows构建软件包;同样,math_386.s将是 仅在为32位x86构建软件包时才包含.

Naming a file dns_windows.go will cause it to be included only when building the package for Windows; similarly, math_386.s will be included only when building the package for 32-bit x86.

使用GOOS = android将构建标签和文件与GOOS = linux中的匹配 除了android标签和文件.

Using GOOS=android matches build tags and files as for GOOS=linux in addition to android tags and files.

使用构建约束.

使用包含多个文件的单个程序包.每个文件都专门针对特定的OS,体系结构等组合.

Use a single package with multiple files. Each file specializes for a particular OS, architecture, etc, combination.

这篇关于我可以根据要构建的OS导入Golang软件包吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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