Build Tags in GoLang
Build Tags in GoLang, sometimes referred to as build constraints, are a potent Go feature that let you include or exclude particular source code files at different points in time throughout the build process. This feature is essential if you want to create many Go application versions from the same source code and customise it for different operating systems, architectures, or even feature sets.
Build Tags in GoLang Purpose and Use Cases
With build tags, developers can:
Target Specific Platforms: Compile code for several underlying processor architectures (GOARCH) and operating systems (GOOS) without changing the primary codebase. This eliminates the need to maintain distinct build machines for every target environment because this capability is directly included into Go’s toolchain.
Manage Feature Sets: Features can be added or removed from the finished binary according to the selected program versions (e.g., Free, Pro, or Enterprise levels of an application). This is more effective than utilising conditional import statements or managing different projects.
Facilitate Integration Testing: During testing, you can quickly transition between code with mock services or stubs and code that is integrated.
Syntax and Placement
A build tag is a particular comment line that must come at the very top and be followed by a blank line. A build tag cannot be preceded by anything, not even other remarks.
A build tag’s fundamental grammar is:
// +build tag_name
The go build command is informed by the +build prefix that this line is a build tag rather than a standard comment. An identifier that you define is the tag_name.
How Build Tags Work in GoLang
When you execute the go build for your program, the compilation will only include files whose build tags meet the predetermined criteria. Using the -tags flag with the go build command, you can enable particular build tags.
For instance, Conditional Features
Think about an app that has “Free” and “Pro” features. Possibly, the free features are defined in the base main.go:
main.go
:
package main
import "fmt"
var features = []string{
"Free Feature #1",
"Free Feature #2",
}
func main() {
for _, f := range features {
fmt.Println(">", f)
}
}
The result of compiling and running this without any special tags (go build then./app) would be:
> Free Feature #1
> Free Feature #2
In order to add Pro features, you would make a distinct file called pro.go and append these features to the features slice using the init() function:
pro.go
:
// +build pro
package main
func init() {
features = append(features,
"Pro Feature #1",
"Pro Feature #2",
)
}
The -tags
flag would be used to compile the program with the Pro features included:
go build -tags pro
./app
After that, both feature sets would be included in the output:
> Free Feature #1
> Free Feature #2
> Pro Feature #1
> Pro Feature #2
Only the Free features would be included if the go build
was executed without the –tags pro
flag, ignoring the pro.go
file.
Boolean Logic with Build Tags
In order to provide more intricate rules for file inclusion, build tags offer simple Boolean logic.
OR Logic (Space-separated): When more than one tag is placed on the same line, an OR relationship is implied. The presence of any of the above tags will result in the inclusion of the file.
This file would be created if the enterprise
or pro
tags were active.
AND Logic (Separate lines): When several tags are placed on different lines, an AND relationship is implied. Only when every tag on the list is present will the file be added.
This file would only be created if the enterprise
and pro
tags were both activated.
Negation (!): By adding an exclamation mark (! ) before a tag, you can make it inverted. In the event that the designated tag is missing, the file will be included.
This tag would provide guidance. You can choose to compile the file for any platform other than Windows.
Using GOOS and GOARCH Filename Suffixes (Implicit Build Tags)
The standard library frequently uses Go’s convention for naming files that implicitly function as build tags to streamline platform-specific code. An explicit // +build
comment can be substituted with _GOOS.go
or _GOOS_GOARCH.go
.
As if it had the // +build windows
, the file path_windows.go, for instance, implicitly informs the Go compiler that it is meant for the Windows operating system. Without requiring explicit build tags in each file, this technique helps organise code by dividing various platform implementations into separate files.
Go applications may be developed in a flexible and structured manner across a variety of platforms and feature requirements with the potent combination of explicit build tags and filename suffixes.
Read more on Effortless Cross Compilation In Go With GOOS And GOARCH