The // +build
tag, used up to Go 1.17, must be followed by an empty line:
// +build main1
package main
Starting from Go 1.17, conditional build tags are able to use //go:build
lines that support boolean expressions instead of the old // +build
lines.
Main improvement
//go:build
comment format is consistent with other go directives as //go:embed
, //go:generate
, //go:noinline
, etc.
- the syntax for boolean expressions between build tags is now standardized, using
&&
and ||
operators
Syntax comparison
Expression |
// +build |
//go:build |
OR |
// +build foo bar (space-separated) |
//go:build foo || bar |
AND |
// +build foo,bar |
//go:build foo && bar |
NOT (unchanged) |
// +build !foo |
//go:build !foo |
Multiline comments
More complex boolean expressions can make use of parenthesis, whereas before it required multiline comments:
From:
// +build foo bar
// +build 386
to:
//go:build (foo || bar) && 386
Additionally, with //go:build
, multiple directives over more than one line are now disallowed.
Automatic formatting
running go fmt
on a source file with a // +build
directive will automatically add the matching //go:build
one.
running go fmt
on a source file with a //go:build
directive in the wrong place will automatically fix it. So now your issue would be solved by simply running gofmt -w main.go
Source: Go 1.17 build constraints draft design. (Currently still a draft even if Go 1.17 is officially released)