| by Arround The Web | No comments

How to Add Golang Build Tags

Build tags in Go are a powerful feature that allows us to conditionally include or exclude a particular code during the build process based on specific build constraints. This flexibility enables us to create the platform-specific builds for different operating systems, architectures, or other environment-specific requirements. Build tags are special comments that are added at the beginning of a source file, just above the package declaration. These tags provide instructions to the Go build system on how to handle the code in that file during the compilation process.

Example 1: Add the Golang Build Tag for Linux System

To target the Linux operating system, we can add the “// +build linux” build tag.

//go build linux

// +build linux

package main

import "fmt"

func main() {

fmt.Println("Hello Linux System example")

}

Here, we set the build constraints in Go which specify that the code should only be built if the target system is Linux. After that, we define the package and import the required statement for the code. Then, we enter the main() method where we print the message using the “Println” call from the “fmt” package.

Thus, the message is displayed on the console successfully for the Linux operating system:

Example 2: Add the Golang Build Tag for Windows System

When we just want the code to execute on the Windows platform, we use the “// +build windows” build tag at the beginning of the file. From that, we can make sure that the code is only incorporated into the build when Windows is the intended platform.

// +build windows

package main

import "fmt"

func main() {

fmt.Println("Hello, Windows example!")

}

Here, we use the “// +build windows” build constraint to ensure that the code is only compiled and executed when building for the Windows platform. After that, we include the necessary package for the code with the package and import modifier. Then comes the main() function which employs the println() function to print the statement when building the window’s tags in Golang.

When we build and run this program on a Windows system, it shows the “Hello, Windows example!” message to the console. Note that the program will not be built or executed on other operating systems due to the build constraint.

Example 3: Add the Golang Build Tag for the Non-Windows System

Conversely, if we want to exclude the code when building for Windows, you can use the “!” exclamation mark with the build tag.

// +build !windows

package main

import "fmt"

func main() {

fmt.Println("Hello, Except Windows!")

}

Here, we build a constraint directive that indicates that the code should be built and executed on platforms other than Windows. The exclamation mark (!) before Windows denotes that this code should be included for all platforms except Windows. Then, we proceed with the same program like in the previous function where we first add the packages and call the main() function to execute the message that is passed inside the println() function.

Thus, the output generates the message on the operating system consoles. Keep in mind that it won’t execute on the Windows operating system due to the build constraint:

Example 4: Add the Golang Build Tag with the OR Logic

To include the code when targeting the Windows OR Linux, we can use the “// +build windows Linux” build tag.

// +build windows Linux

package main

import "fmt"

func main() {

fmt.Println("Hey, Windows or Linux Users")

}

Here, we apply the build constraint with the logical OR which intends to be built for both Windows and Linux operating systems. The “// +build windows Linux” line specifies that this file should be included in the build when the target platform is either Windows or Linux. Next, we import the “fmt” package from the Go standard library which provides the basic input and output functionality. In the main function, we print the “Hey, Windows or Linux Users” string statement using the Println() function from the “fmt” package.

Hence, the output is retrieved in the following. The same message is generated whether we use the Windows or Linux operating system:

Example 5: Add the Golang Build Tag with the AND Logic

We can specify multiple build tags to create more complex conditions. The “// +build windows,linux” command can be used to generate a code for both Linux and Windows.

// +build windows,386

package main

import "fmt"

func main() {

fmt.Println("This is 32-bit Windows")

}

Here, the build constraint that is defined is “// +build windows,386” which specifies that the Go file should be included in the build when the target platform is a 32-bit Windows. Note that we use a comma “,” in between Windows and 386 which acts as the logical AND operator. This means that both conditions must be satisfied for the code to be included in the build.

Then, similar to the previous example, we import the “fmt” package from the Go standard library to use the Println function() for output. We have the main() function of the program where the statement is passed as an argument to the println() function.

When we build and run this program on a 32-bit Windows system, it displays the specified message as the output. However, if we attempt to build and run it on a different operating system or architecture, it will not be included in the build:

Example 6: Add the Golang Build Tag for a Specific Function

Moreover, build tags can also be applied to specific functions within a file. Because of the build constraints, we can now conditionally include or remove specific functions.

package main

import "fmt"

func main() {

fmt.Println("Hey there, Enjoy!")

printWindowsMessage()

}

// +build windows

func printWindowsMessage() {

fmt.Println("Hello From Windows Operating system")

}

Here, we now move on to the main() method which uses the Println() method provided by the “fmt” package to print the message in the beginning. Then, we deploy the printWindowsMessage() function below the main() function and specify the “// +build windows” build constraint. This indicates that the function is only included in the build when the target platform is Windows.

Finally, we set the printWindowsMessage() function to print the specified “Hello From Windows Operating system” message using the Println() function.

Thus, the output is retrieved which displays both the general message and the platform-specific message. Note that if we build and run it on a different operating system, the printWindowsMessage() function will not be included in the build, and only the general message will be displayed:

Conclusion

Adding build tags in Go provides the ability to customize the code inclusion during the build process. By leveraging the build tags, we can create the platform-specific builds, optimize the code for different architectures, and even conditionally include specific functions or features.

Share Button

Source: linuxhint.com

Leave a Reply