go build条件编译

官方信息

pkg/go/build

build tag

A build constraint, also known as a build tag, is a line comment that begins
// +build
that lists the conditions under which a file should be included in the package
必须在文件的package声明前面

// +build linux,386 darwin,!cgo
corresponds to the boolean formula:

(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:

// +build linux darwin
// +build 386
corresponds to the boolean formula:

(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
- "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
- "go1.11", from Go version 1.11 onward
- "go1.12", from Go version 1.12 onward
- "go1.13", from Go version 1.13 onward
- any additional words listed in ctxt.BuildTags// go build -tags 'tag list'

To keep a file from being considered for the build:

// +build ignore

文件后缀

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

*_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).
发布了91 篇原创文章 · 获赞 0 · 访问量 822

猜你喜欢

转载自blog.csdn.net/weixin_45594127/article/details/104070329