Documentation
¶
Overview ¶
Command buildtag adds a build constraint to Go source files: it ANDs the given expression into each file's //go:build line, or writes that line when the file has none.
buildtag -and '!rbgo_core' a.go b.go …
It exists because a build tag across a few hundred files is an edit nobody should make by hand or with a stream editor. go-embedded-ruby's rbgo_core build needed it on 288 gem-binding files: leaving a gem's register function uncalled does NOT drop the gem from the binary, because every imported package's init runs, and the linker keeps it and everything it reaches. Only a file that is not compiled stops importing.
Each file is handled the way gofmt expects:
- an existing //go:build line X becomes `X && EXPR`, parenthesised by go/build/constraint, so `!(js && wasm)` becomes `!(js && wasm) && !tag`;
- a file already carrying EXPR as one of its conjuncts is left alone, so a second run changes nothing;
- a new line goes after the leading comment block when a blank line closes it (a licence header), otherwise at the top, and is always followed by a blank line, so it can never be read as part of a package doc comment.
It refuses, and writes nothing at all, when any file cannot be handled safely: a legacy "// +build" line (gofmt keeps the two in step, a tool adding to one would split them), two //go:build lines, a /* */ comment before the package clause, or a result go/parser cannot read. Every file is checked before any is written. -n lists what would change and writes nothing.