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
+10.9k Google Maps URL parameters configuration
+27.2k PHP : Convert(cast) string to bigInt
+6.2k CodeIgniter : form input set_value cause " to become & quot
+14.2k Golang : Send email with attachment(RFC2822) using Gmail API example
+11.7k Golang : How to parse plain email text and process email header?
+23.8k Golang : Find biggest/largest number in array
+9.8k Golang : Setting variable value with ldflags
+3.9k Detect if Google Analytics and Developer Media are loaded properly or not
+5.2k Golang *File points to a file or directory ?
+18.3k Golang : Find IP address from string