Documentation ¶
Overview ¶
Package glob provides rudimentary pattern matching functions using shell-like wildcards `*` and `?`.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrInvalidGlobSequence = errors.New("* or ? may not follow *")
ErrInvalidGlobSequence is returned by NewPattern if the glob pattern contained any wildcard following an asterisk.
var ErrInvalidPatternType = errors.New("invalid pattern type")
ErrInvalidPatternType is returned by Matches if the given pattern type was neither a string nor a *GlobPattern.
var ErrPatternEmpty = errors.New("compiled glob pattern is empty")
ErrPatternEmpty is returned by NewPattern if the resulting pattern is empty.
var ErrPatternInvalid = errors.New("unable to compile glob pattern")
ErrPatternInvalid is returned by NewPattern if pattern compilation failed without an error.
Functions ¶
func Matches ¶
Matches returns whether the glob pattern matches str. If an error occurs (i.e., the pattern is somehow invalid), it will always return false and an error. Otherwise, if the pattern is valid, it will return true or false depending on whether it matches str and a nil error.
pattern may be either a *GlobPattern or a PatternStr. If it's a string, it will be parsed and compiled on demand.
If the pattern is neither a string nor a *GlobPattern, ErrInvalidPatternType will be the returned error.
If pattern is a string and an error is returned, it is any error that may be returned by NewPattern.
Example ¶
var m bool var err error // Match m, err = Matches(PatternStr("foo/bar/*/baz"), "foo/bar/qux/baz") switch { case err != nil: panic(err) case m: fmt.Println("Matches!") } // No match m, err = Matches(PatternStr("foo/bar/*/baz?"), "foo/bar/qux/baz") switch { case err != nil: panic(err) case !m: fmt.Println("Doesn't match!") }
Output: Matches! Doesn't match!
Types ¶
type GlobPattern ¶
type GlobPattern struct {
// contains filtered or unexported fields
}
GlobPattern is a compiled glob pattern.
func NewPattern ¶
func NewPattern(pattern string) (*GlobPattern, error)
NewPattern allocates a new GlobPattern based on pattern and returns it. Patterns consist of varying sequences of chars interspersed with wildcards -- either `*` or `?` to match 1 or more characters or a single character, respectively. Any character may be escaped with a backslash (\) to produce the same literal character in the string. Escaping any other character will yield the escaped character. Avoid escaping characters where possible, as this introduces additional complexity into the pattern.
Example ¶
var p *GlobPattern var err error p, err = NewPattern("foo/bar/*/baz") if err != nil { panic(err) } m := p.Matches("foo/bar/quz/baz") if m { fmt.Println("Matches!") } m = p.Matches("foo/bar/") if !m { fmt.Println("Doesn't match!") }
Output: Matches! Doesn't match!
func (*GlobPattern) Matches ¶
func (p *GlobPattern) Matches(str string) bool
Matches returns whether the glob pattern p matches str.
func (*GlobPattern) String ¶
func (p *GlobPattern) String() string
String returns the pattern this GlobPattern was compiled with.
type Pattern ¶
type Pattern interface {
// contains filtered or unexported methods
}
Pattern is the common interface implemented by patterns under the glob package. Only PatternStr and GlobPattern implement this, which allows them to be recognized as patterns by Matches(). When in doubt, using the concrete GlobPattern is recommended.
type PatternStr ¶
type PatternStr string
PatternStr is a convenience type for passing strings as patterns to the Matches function. The PatternStr is compiled on demand.