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
+7.3k Golang : File system scanning
+19.4k Golang : How to count the number of repeated characters in a string?
+26.5k Golang : Get executable name behind process ID example
+11.6k Swift : Convert (cast) Float to String
+6.7k Golang : Reverse by word
+32.8k Golang : Regular Expression for alphanumeric and underscore
+13.1k Golang : List objects in AWS S3 bucket
+26.9k Golang : Find files by extension
+7.3k Golang : How to fix html/template : "somefile" is undefined error?
+25.8k Golang : missing Mercurial command
+6.1k Golang : Missing Subversion command
+15.3k JavaScript/JQuery : Detect or intercept enter key pressed example