Documentation ¶
Overview ¶
Package buildtags provides types for representing and manipulating build constraints.
In Go, build constraints are represented as comments in source code together with file naming conventions. For example
// +build linux,386 darwin,!cgo // +build !purego
Any terms provided in the filename can be thought of as an implicit extra constraint comment line. Collectively, these are referred to as “constraints”. Each line is a “constraint”. Within each constraint the space-separated terms are “options”, and within that the comma-separated items are “terms” which may be negated with at most one exclaimation mark.
These represent a boolean formulae. The constraints are evaluated as the AND of constraint lines; a constraint is evaluated as the OR of its options and an option is evaluated as the AND of its terms. Overall build constraints are a boolean formula that is an AND of ORs of ANDs.
This level of complexity is rarely used in Go programs. Therefore this package aims to provide access to all these layers of nesting if required, but make it easy to forget about for basic use cases too.
Index ¶
- func Format(t ConstraintsConvertable) (string, error)
- func GoBuildSyntaxSupported() bool
- func PlusBuildSyntaxSupported() bool
- func SetTags(idents ...string) map[string]bool
- type Constraint
- type ConstraintConvertable
- type Constraints
- type ConstraintsConvertable
- type Interface
- type Option
- type OptionConvertable
- type Term
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Format ¶ added in v0.3.0
func Format(t ConstraintsConvertable) (string, error)
Format constraints according to the syntax supported by the current Go version.
func GoBuildSyntaxSupported ¶ added in v0.3.0
func GoBuildSyntaxSupported() bool
GoBuildSyntaxSupported reports whether the current Go version supports the "//go:build" constraint syntax.
func PlusBuildSyntaxSupported ¶ added in v0.3.0
func PlusBuildSyntaxSupported() bool
PlusBuildSyntaxSupported reports whether the current Go version supports the "// +build" constraint syntax.
Types ¶
type Constraint ¶
type Constraint []Option
Constraint represents the OR of a list of Options.
func Any ¶
func Any(opts ...OptionConvertable) Constraint
Any builds a Constraint that will be true if any of its options are true.
func ParseConstraint ¶
func ParseConstraint(expr string) (Constraint, error)
ParseConstraint parses a space-separated list of options.
Example ¶
package main import ( "fmt" "github.com/mmcloughlin/avo/buildtags" ) func main() { c, err := buildtags.ParseConstraint("a,!b c") fmt.Print(c.GoString()) fmt.Println(err) }
Output: // +build a,!b c <nil>
func (Constraint) Evaluate ¶
func (c Constraint) Evaluate(v map[string]bool) bool
Evaluate the boolean formula represented by c under the given assignment of tag values. This is the OR of the values of the constituent Options.
func (Constraint) GoString ¶
func (c Constraint) GoString() string
GoString represents the Constraint as one +build comment line.
func (Constraint) ToConstraint ¶
func (c Constraint) ToConstraint() Constraint
ToConstraint returns c.
func (Constraint) ToConstraints ¶
func (c Constraint) ToConstraints() Constraints
ToConstraints returns the list of constraints containing just c.
func (Constraint) Validate ¶
func (c Constraint) Validate() error
Validate validates the constraint.
type ConstraintConvertable ¶
type ConstraintConvertable interface {
ToConstraint() Constraint
}
ConstraintConvertable can be converted to a Constraint.
type Constraints ¶
type Constraints []Constraint
Constraints represents the AND of a list of Constraint lines.
func And ¶
func And(cs ...ConstraintConvertable) Constraints
And builds Constraints that will be true if all of its constraints are true.
func (Constraints) Evaluate ¶
func (cs Constraints) Evaluate(v map[string]bool) bool
Evaluate the boolean formula represented by cs under the given assignment of tag values. This is the AND of the values of the constituent Constraints.
func (Constraints) GoString ¶
func (cs Constraints) GoString() string
GoString represents Constraints as +build comment lines.
func (Constraints) ToConstraints ¶
func (cs Constraints) ToConstraints() Constraints
ToConstraints returns cs.
func (Constraints) Validate ¶
func (cs Constraints) Validate() error
Validate validates the constraints set.
type ConstraintsConvertable ¶
type ConstraintsConvertable interface {
ToConstraints() Constraints
}
ConstraintsConvertable can be converted to a Constraints object.
type Interface ¶
type Interface interface { ConstraintsConvertable fmt.GoStringer Evaluate(v map[string]bool) bool Validate() error }
Interface represents a build constraint.
type Option ¶
type Option []Term
Option represents the AND of a list of Terms.
func ParseOption ¶
ParseOption parses a comma-separated list of terms.
func (Option) Evaluate ¶
Evaluate the boolean formula represented by o under the given assignment of tag values. This is the AND of the values of the constituent Terms.
func (Option) ToConstraint ¶
func (o Option) ToConstraint() Constraint
ToConstraint returns a Constraint containing just this option.
func (Option) ToConstraints ¶
func (o Option) ToConstraints() Constraints
ToConstraints returns Constraints containing just this option.
type OptionConvertable ¶
type OptionConvertable interface {
ToOption() Option
}
OptionConvertable can be converted to an Option.
type Term ¶
type Term string
Term is an atomic term in a build constraint: an identifier or its negation.
func (Term) ToConstraint ¶
func (t Term) ToConstraint() Constraint
ToConstraint returns a Constraint containing just this term.
func (Term) ToConstraints ¶
func (t Term) ToConstraints() Constraints
ToConstraints returns Constraints containing just this term.