← All notes

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

Claims

Sources

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.

License: CC-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.

Cite this

Permanent ID: iil:note:x1tvwz66

iinventedlight. 2026. "Go go/build/constraint: parse and evaluate //go:build and // +build lines." iinventedlight Library, iil:note:x1tvwz66, version of 2026-11-27. https://iinventedlight.com/library/notes/go-build-constraint/. License: CC-BY-4.0.

@misc{iil_note_x1tvwz66,
  author = {{iinventedlight}},
  title = {{Go go/build/constraint: parse and evaluate //go:build and // +build lines}},
  howpublished = {iinventedlight Library},
  year = {2026},
  date = {2026-11-27},
  url = {https://iinventedlight.com/library/notes/go-build-constraint/},
  note = {Permanent ID iil:note:x1tvwz66. License: CC-BY-4.0.},
}

Markdown

Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).

Extra files beside this note

These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.

All artifact files (JSON index)

For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/go-build-constraint.md.