Module lessons (5/5)
Build constraints and tags
The //go:build ... directive for cross-platform, separate test/integration code, real examples.
What you'll learn in this lesson
- Declare modern constraints with
//go:buildrespecting position and blank line beforepackage. - Combine OS, architectures, Go versions, and custom tags with the
&&,||, and!operators. - Take advantage of the implicit convention of filename suffixes (
_linux.go,_windows.go). - Enable custom tags during build and testing via
-tags.
Overview
When you compile the same program for Linux, macOS, Windows or want to separate quick unit tests from heavy integration tests, Go provides build constraints (or build tags): rules evaluated at compile-time that include or exclude entire source files.
Introduced in Go 1.17 to replace the old // +build, the directive uses standard Boolean expressions before the package clause:
Sections covered in this lesson
- 1Modern Syntax
//go:build - 2File name suffix convention
- 3Custom opt-in tag (e.g. test
integration) - 4The critical blank line rule
- 5Guided practice
- 6Check for understanding
Unlock "Build constraints and tags" and the full Course Go curriculum
The `//go:build ...` directive for cross-platform, separate test/integration code, real examples.
Unlock all 5 hands-on exercises in this lesson plus every advanced WebAssembly module with automated checks.
- Instant access to all advanced modules in Course Go plus 17 more courses
- Hands-on browser exercises with automated test suites and solutions
- 14-day money-back guarantee processed via Paddle.com
14-day guarantee · Cancel anytimeModule 1, all 18 interactive games, and cheatsheets are always free.