liveBook · Manning

Build tag

As explained in Go in Practice, Second Edition

A Go build tag is a source-file constraint that determines whether the file is included for a particular build environment.

A build tag is a source-file constraint that tells the Go toolchain whether to include a file for a particular build environment. Place the special comment at the start of the file and follow it with a blank line. section 12.4.2

Use build tags and environment-specific filenames to keep platform differences behind separate implementations:

  • Use tags such as !windows or !linux,!darwin to exclude files from selected operating systems. section 12.4.2
  • Relate tag values to GOOS and GOARCH settings when compiling for different operating systems and architectures. section 12.4.2
  • Name files with suffixes such as foo_windows.go or foo_386.go to select platform- or architecture-specific code without an explicit tag. section 12.4.2
$ GOOS=windows GOARCH=386 go build

Cross-compile a 386 Windows build by setting GOOS and GOARCH before running go build. section 12.4.2

How Go in Practice, Second Edition covers it

How deep it goes
Explains tag placement, exclusion values, GOOS and GOARCH linkage, and filename conventions.
Examples
Demonstrates !windows and !linux,!darwin tags, foo_windows.go and foo_386.go filenames, and a Windows 386 build command.
What you can do after
Operate/configure Go builds that include appropriate files for selected operating systems and hardware architectures.
How it connects
Connects build tags with cross-compilation, GOOS, GOARCH, and platform-specific filenames.

Also explained in

Compare how 3 books explain build tag →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Go in Practice, Second Edition. The section links point to the parts of the book it draws on.