Build tags for targeted platforms
Built tags, or build constraints, can be used for many purposes, but in this section, we will be discussing how to use build tags to identify which files should be included in a package when building for specific operating systems. Build tags are given in a comment at the top of a file:
//go:build
Build tags are passed in as flags when running go build
. There could be more than one tag on a file, and they follow on from the comment with the following syntax:
//go:build [tags]
Each tag is separated by a space. Suppose we want to indicate that this file will only be included in a build for the Darwin operating system, then we would add this to the top of the file:
//go:build darwin
Then when building the application, we would use something like this:
go build –tags darwin
This is just a super quick overview of how build tags can be used to constrain files specific to operating systems. Before we go into an implementation...