Build Constraints(约束)

Build -tags

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

1
// +build

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.

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:

1
// +build linux,386 darwin,!cgo

corresponds(对应于) to the boolean formula:

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

A file may have multiple build constraints. The overall(总体) constraint is the AND of the individual(单独的) constraints. That is, the build constraints:

1
2
// +build linux darwin
// +build 386

During a particular build, the following words are satisfied:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
- 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
- "go1.7", from Go version 1.7 onward
- "go1.8", from Go version 1.8 onward
- "go1.9", from Go version 1.9 onward
- "go1.10", from Go version 1.10 onward(向前)
- any additional words listed in ctxt.BuildTags

Note:A build tags follow these three rules - a build tag is evaluated as the OR of space-separated options(空格分割,OR) - each option evaluates as the AND of its comma-separated terms(逗号分隔,AND) - each term is an alphanumeric word or, preceded by !, its negation(!, 否定)

File Suffixes

If a file’s name, after stripping(剥离) the extension(扩展名) and a possible _test suffix, matches any of the following patterns:

1
2
3
*_GOOS
*_GOARCH
*_GOOS_GOARCH

*_GOOS_GOARCH 中 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:

1
// +build ignore

(any other unsatisfied(不满意) word will work as well, but “ignore” is conventional(常规的). 临时让某个文件不参与编译)

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

1
// +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:

1
// +build !linux,!darwin !cgo

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.

Using GOOS=android matches build tags and files as for GOOS=linux in addition to android tags and files.(当使编译Android平台的时候,会把Linux相关的文件一起加入进行编译,优先编译了_linux.go文件,然后再到_android.go文件)

See Also

Thanks to the authors 🙂