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
+5.8k Golang : Warp text string by number of characters or runes example
+4.6k Clean up Visual Studio For Mac installation failed disk full problem
+3.9k Unix/Linux : How to pipe/save output of a command to file?
+5.8k Golang : Check if password length meet the requirement
+6.3k Golang : Gorrila mux.Vars() function example
+6.1k Golang : Fibonacci number generator examples
+4.7k Golang : ROT32768 (rotate by 0x80) UTF-8 strings example
+4.6k Golang : Display advertisement images or strings on random order
+10.5k Golang : Surveillance with web camera and OpenCV
+4.9k Golang : Denco multiplexer example
+4.6k How to check with curl if my website or the asset is gzipped ?
+4.5k Golang : Reclaim memory occupied by make() example