---
source: https://iinventedlight.com/library/notes/go-build-constraint.md
title: "Go go/build/constraint: parse and evaluate //go:build and // +build lines"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "BSD-3-Clause (The Go Authors); BSD-3-Clause (The Go Authors); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "Summary by iinventedlight (AI-assisted). Sources: The Go Authors, constraint package - go/build/constraint; The Go Authors, expr.go (source). BSD-3-Clause (https://go.dev/LICENSE). Copyright The Go Authors."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-27
status: published
approved_by: "Cole Withington, 2026-11-27 14:30 PT: \"Yes, publish all of that now\""
checked_on: 2026-11-27
as_of: 2026-11-27
confidence: medium
untested: "Not reproduced (2026-11-27). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-systems: go-build-constraint"
tags: [go, build-tags, go-build, constraint, expr]
client: Librarian
sources:
  - id: S1
    title: "constraint package - go/build/constraint"
    publisher: The Go Authors
    url: https://pkg.go.dev/go/build/constraint
    accessed: 2026-11-27
    license: "BSD-3-Clause"
  - id: S2
    title: "expr.go (source)"
    publisher: The Go Authors
    url: https://go.dev/src/go/build/constraint/expr.go
    accessed: 2026-11-27
    license: "BSD-3-Clause"
---
# Go go/build/constraint: parse and evaluate //go:build and // +build lines
**Answer:** Package constraint implements parsing and evaluation of build constraint lines, including //go:build and // +build.

## Parsing both syntaxes

The package is about the constraint lines themselves. See the go command documentation about build constraints themselves.

It accepts the original // +build form and the //go:build form added in Go 1.17.

Parse reads a single line of either form and returns a boolean Expr. A SyntaxError reports a problem at a byte offset in the input.

IsGoBuild reports whether a line is a //go:build constraint. IsPlusBuild reports whether it is a // +build constraint. Both only inspect the prefix; they do not parse the expression.

An Expr is a TagExpr, NotExpr, AndExpr, or OrExpr. String prints the expression in //go:build boolean syntax. Eval reports whether it is true by calling ok(tag) as needed.

A TagExpr holds a single tag, for example linux or cgo. A NotExpr represents the expression !X. An AndExpr represents X && Y. An OrExpr represents X || Y. Tags must be letters, digits, underscores or dots.

## Version and conversion helpers

GoVersion, added in go1.21.0, returns the minimum Go version implied by an expression, such as go1.22 for linux && go1.22. If the expression can be satisfied without any Go version tags, it returns an empty string, as with a bare linux tag or !go1.22.

The analysis is structural and does not use SAT solving, so impossible subexpressions can still affect the result.

PlusBuildLines returns // +build lines that evaluate to a given expression, or an error if the expression is too complex to convert directly.

## See also

- [go-build](https://iinventedlight.com/library/notes/go-build/)
- [go-build-tags](https://iinventedlight.com/library/notes/go-build-tags/)
- [go-ast](https://iinventedlight.com/library/notes/go-ast/)

## Claims

- id: C1
  claim: The package parses and evaluates build constraint lines.
  source: S1
  evidence: "implements parsing and evaluation of build constraint lines"

- id: C2
  claim: It parses both // +build and the //go:build syntax added in Go 1.17.
  source: S1
  evidence: "syntax that was added in Go 1.17"

- id: C3
  claim: IsGoBuild only checks the //go:build prefix, not that the expression parses.
  source: S2
  evidence: "It only checks the prefix of the text, not that the expression itself parses"

## Sources

- [S1] constraint package - go/build/constraint. The Go Authors. https://pkg.go.dev/go/build/constraint . License: BSD-3-Clause. Accessed 2026-11-27.

- [S2] expr.go (source). The Go Authors. https://go.dev/src/go/build/constraint/expr.go . License: BSD-3-Clause. Accessed 2026-11-27.

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: The Go Authors, constraint package - go/build/constraint; The Go Authors, expr.go (source). BSD-3-Clause (https://go.dev/LICENSE). Copyright The Go Authors. Text was condensed and changed from the originals.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
