Go Build Tags: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 22: Line 22:
Multiple tags are supported, and they can be combined using an OR, AND or NOT logic.
Multiple tags are supported, and they can be combined using an OR, AND or NOT logic.
==File will be Included if Either Tags are Specified During Compilation==
==File will be Included if Either Tags are Specified During Compilation==
When the tags are separated by spaces in the declaration, is sufficient that any of them is present for the file to be compiled. The following declaration has the semantics:
When the tags are separated by spaces in the declaration, is sufficient that any of them is present for the file to be compiled. The following declaration:
<font size=-1>
tag_a OR tag_b OR tag_c
</font>
<syntaxhighlight lang='go'>
<syntaxhighlight lang='go'>
// +build tag_a tag_b tag_c
// +build tag_a tag_b tag_c
Line 31: Line 28:
package somepkg
package somepkg
</syntaxhighlight>
</syntaxhighlight>
has the semantics:
<font size=-1>
tag_a OR tag_b OR tag_c
</font>


==File will be Included if All Tags are Specified During Compilation==
==File will be Included if All Tags are Specified During Compilation==

Revision as of 22:24, 7 March 2024

Internal

Overview

A build tag is an identifier added to a piece of code that determines when the file should be including in a package during the build process.

Build tags provide conditional compilation in Go. Go does not have a preprocessor, a macro system, or a #define declaration to control the inclusion of platform-specific code, it uses build tags instead.

Build tags are implemented as comments which must appear at the top of the file in any source file (not just go files). To differentiate between the package and build tags, there must be a blank line between them:

// +build tag_a tag_b

package somepkg

Providing Tags on Command Line

go build|test -tags=tag_a [...]

Combining Tags

Multiple tags are supported, and they can be combined using an OR, AND or NOT logic.

File will be Included if Either Tags are Specified During Compilation

When the tags are separated by spaces in the declaration, is sufficient that any of them is present for the file to be compiled. The following declaration:

// +build tag_a tag_b tag_c

package somepkg

has the semantics:

tag_a OR tag_b OR tag_c

File will be Included if All Tags are Specified During Compilation

When the tags are separated by a comma in the declaration, all of them must be present . The following declaration:

// +build tag_a,tag_b,tag_c

package somepkg

has the semantics:

tag_a AND tag_b AND tag_c