Golang : Use wildcard patterns with filepath.Glob() example
Searching for files by extension can be done easily in Golang with filepath.Ext()
function. To search for files base on wildcard pattern can be done easily too with filepath.Glob()
function.
In this code example, we will use filepath.Glob()
function with a wildcard pattern to list out files with the string input
in the file names.
package main
import (
"fmt"
"path/filepath"
)
func main() {
pattern := "*input*"
matches, err := filepath.Glob(pattern)
if err != nil {
fmt.Println(err)
}
fmt.Println(matches)
// search upper directory
upperDirPattern := "../*input*"
// you can specify directly the directory you want to search as well with the pattern
// for example, /usr/files/*input*
matches, err = filepath.Glob(upperDirPattern)
if err != nil {
fmt.Println(err)
}
fmt.Println(matches)
}
Sample output:
[checkinput checkinput.go input.gif input.txt scrinputtofile scrinputtofile.go]
[../testinputfile]
Reference:
See also : Golang : Find files by extension
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+4.8k Chrome : How to block socketloop.com links in Google SERP?
+6k Cash Flow : 50 days to pay your credit card debt
+9k Golang : Take screen shot of browser with JQuery example
+13.3k Golang : Convert(cast) uintptr to string example
+10.3k Golang : Identifying Golang HTTP client request
+6.5k PHP : How to handle URI or URL with non-ASCII characters such as Chinese/Japanese/Korean(CJK) ?
+5.4k PHP : Hide PHP version information from curl
+10.8k Golang : Allow Cross-Origin Resource Sharing request
+4.4k Javascript : How to show different content with noscript?
+8.2k Golang : Handle Palindrome string with case sensitivity and unicode
+7.5k Golang : Calculate how many weeks left to go in a given year
+17.8k Convert JSON to CSV in Golang