SoFunction
Updated on 2025-03-05

Go compile tag build tag comment details syntax explanation

1. Introduction

In Go, build tag is added to the first line in our code to identify the compilation-related information.

It determines whether the current file will be included in the current package.

Used to limit whether an entire file should be compiled into the final binary file, rather than part of the code snippet in a file

The syntax of go compile tags (build tags) is as follows:

// +build [tag]
  • Near the top of the Build tags file, there can only be blank lines and other line comments in front.
  • The compile tag must appear before the package clause and to be distinguished from the package document, it must be followed by a blank line.

2. Build tags bool logic

When we use multiple tags in a package, they interact with Boolean logic, depending on how we declare.

Build tags follow the following three rules:

  • Space-separated labels will be interpreted under OR logic.
  • Comma-separated tags will be interpreted under AND logic.
  • Each term is an alphanumeric word, if preceded! It means it is denied.

2.1. or tag logic

Given tag:

// +build tag1 tag2

The OR explanation is that if tag1 or tag2 exists when executing the build command, this file will be included.

2.2. and tag logic

If we use the tag:

// +build tag1, tag2

The explanation is tag1 and (AND) tag2 must exist in the build build command before our file can be included in the compile.

2.3.! (Non-label logic)

If we use tags

// +build !tag1

The explanation is that only if our files are not tag1, our files will be compiled.

3. How to use

3.1 Create a new build tag

We create a new buildtag folder and create the following 4 empty files under the folder.

.
├── 
├── 
├── 
└── 

Let's open the input code as follows:

package main
import "fmt"
var configArr []string
func main() {
    for _, conf := range configArr {
        (conf)
    }
}

Let's open the input code as follows:

// +build dev
package main
func init() {
    configArr = append(configArr, "mysql dev")
}

Let's open the input code as follows:

// +build prod
package main
func init() {
    configArr = append(configArr, "mysql prod")
}

Let's open the input code as follows:

// +build test1
package main
func init() {
    configArr = append(configArr, "mysql test")
}

3.2 Compilation with tags

3.2.1 Simple compilation with single tags

We use

go build  -tags "dev"

Generated binary executable file in the folder buildtag

Let's do it:

➜ ./buildtag

Output:

mysql dev

3.2.2 Multiple relationship compilation

We use

go build  -tags "dev prod"

Generated binary executable file in the folder buildtag

Let's do it:

➜ ./buildtag

Output:

mysql dev
mysql prod

4. The difference between go:build and +build

//go:build

It is a new conditional compilation directive format introduced in Go 1.17. It is designed to replace

// +build

instruction. Why adopt a new format?

Comparing the differences between the old and new formats, you will know:

// go:build linux && amd64 || darwin
// +build linux,amd64 darwin

The obvious advantages:

The format go:build is easier for coder to understand its logical combinations compared with the commands //go:embed and //go:generate, and the format is unified.

Other study materials

gin realizes switching of json package through go build -tags

The above is the detailed explanation of the syntax in the go compiled tag build tag annotation. For more information about the grammar of go compiled tag build tag annotation, please follow my other related articles!