I Spy Code - GO

Match shell file name pattern

Question:

How do you test if a path matches a shell file name pattern in golang?

Answer:

The function path.Match() reports whether name matches the shell file name pattern.

Here is a go lang example that test is a path matches a shell file name pattern.

Source: (example.go)

package main
 
import (
   "fmt"
   "path"
)
 
func main() {
 
   mypath := "/Users/dennis/ISPY/GO/example.go"
 
   results ,_ := path.Match("/*/*/*/*/*.go", mypath)
   fmt.Println("Match:",results)
 
   results ,_ = path.Match("/*/*/*/*/exampl?.go", mypath)
   fmt.Println("Match:",results)
}
 

Output:

$ go run example.go
Match: true
Match: true

References:

https://golang.org/pkg/path/#Match