Skip to main content
Module 9 · Lesson 5 of 545/50 in the course~10 min
Module lessons (5/5)

Build constraints and tags

Lesson preview5 hands-on exercises readyCourse Go · Module 9 — Testing, modules and tooling

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:build respecting position and blank line before package.
  • 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

  1. 1Modern Syntax //go:build
  2. 2File name suffix convention
  3. 3Custom opt-in tag (e.g. test integration)
  4. 4The critical blank line rule
  5. 5Guided practice
  6. 6Check for understanding
Pro Lesson · Module 9 — Testing, modules and tooling

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.