Build Tags in Go

Build Tags in Go

Build Tags in Go is an identifier ( in the form of a single line comment) added to the top level of your file, which tells the Go Compiler to what to do with the file during the build process. Let us see how we can create a build tag :).

We created a file called name.go and if you see at the top of the file you can find a comment line, this is called a build tag, name is the build tag we specified for this file. By Default, This file will not be considered if you do go build.

To include this file in your build, We need to add tags to our build command. Eg: go build -tags=name

We can give multiple tags to a file and which interact with each other based on boolean logic.

Multiple Build Tags Boolean Logic

Also, multiple line tags are considered as an AND condition.

Build Tags in Testing

Build Tags are also used in running tests in golang. The above way is required just add the build tag in your _test.go file. And run it using go test -tags=integration .

Reference

build - The Go Programming Language
Package build gathers information about Go packages. The Go path is a list of directory trees containing Go source…golang.org

Separate Your Go Tests with Build Tags
Have you ever wanted to separate your unit tests from your integration or smoke tests? Go has a built-in mechanism for…mickey.dev