- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Golang Program to get the List of filenames matching the specified wild card pattern
One of the features of Go is the ability to search and retrieve files based on a specified pattern. In this article, we will discuss different methods to get the list of filenames that match a specified wildcard pattern in Go, and provide syntax and examples for each method.
Method 1: Using the "filepath.glob"
The "filepath.Glob" method is the easiest and most straightforward way to retrieve the list of filenames that match a specified wildcard pattern. The function takes a pattern as input and returns a slice of strings that match the pattern.
Syntax
filenames, err := filepath.Glob(pattern)
The "filepath.Glob" function takes a string parameter pattern, which is the wildcard pattern you want to match. The function returns two values: files which is a slice of strings representing the filenames that match the pattern, and err which is an error value. If the function call is successful, err will be nil.
Algorithm
Step 1 − Import the "path/filepath" package in your Go program.
Step 2 − Define a pattern variable with the wildcard pattern you want to match.
Step 3 − Call the "filepath.Glob" function with the pattern as input, and store the returned result in a variable.
Step 4 − Check if there was an error while calling the "filepath.Glob" function. If there was an error, print the error message and return.
Step 5 − Print the list of filenames that match the pattern.
Example
In this example, we are going to use an internal package of Golang called filepath.glob to get the list of filenames matching the specified wild card pattern
package main import ( "fmt" "path/filepath" ) func main() { pattern := "*.txt" files, err := filepath.Glob(pattern) if err != nil { fmt.Println("Error:", err) return } fmt.Println("Files:", files) }
Output
Files: []
Method 2: Using "ioutil.ReadDir"
The "ioutil.ReadDir" method can also be used to retrieve a list of filenames that match a specified pattern. The function takes a directory path as input and returns a slice of "os.FileInfo" structs that represent the files in the directory. We can then filter the files based on the pattern and retrieve the filenames.
Syntax
ioutil.ReadDir(dir)
The "ioutil.ReadDir" function takes a string parameter dirPath, which is the path to the directory you want to read. The function returns two values: files which is a slice of os.FileInfo objects representing the files in the directory, and err which is an error value. If the function call is successful, err will be nil.
filepath.Match(pattern, file.Name())
The "filepath.Match" function takes two string parameters: pattern, which is the wildcard pattern you want to match, and name, which is the filename you want to match against the pattern. The function returns two values: match which is a boolean value indicating whether the pattern and the filename match, and err which is an error value. If the function call is successful, err will be nil.
Algorithm
Step 1 − Import the "io/ioutil" and "path/filepath" packages in your Go program.
Step 2 − Define a pattern variable with the wildcard pattern you want to match.
Step 3 − Call the "ioutil.ReadDir" function with the directory path as input, and store the returned result in a variable.
Step 4 − Check if there was an error while calling the "ioutil.ReadDir" function. If there was an error, print the error message and return.
Step 5 − Initialize an empty slice of strings to store the filenames.
Step 6 − Loop through each file in the returned result from "ioutil.ReadDir".
Step 7 − Call the "filepath.Match" function with the pattern and the filename as input, and store the result in a variable.
Step 8 − If the result of "filepath.Match" is true, add the filename to the slice of filenames.
Step 9 − Repeat Steps 6 to 8 for all files in the returned result from "ioutil.ReadDir".
Step 10 − Print the list of filenames that match the pattern.
Example
In this example, we are going to use an internal package of Golang called ioutil.ReadDir to get the list of filenames matching the specified wild card pattern
package main import ( "fmt" "io/ioutil" "path/filepath" ) func main() { pattern := "*.go" files, err := ioutil.ReadDir(".") if err != nil { fmt.Println("Error:", err) return } var matchingFiles []string for _, file := range files { match, err := filepath.Match(pattern, file.Name()) if err != nil { fmt.Println("Error:", err) return } if match { matchingFiles = append(matchingFiles, file.Name()) } } fmt.Println("Files:", matchingFiles) }
Output
Files: [main.go]
Conclusion
These are two of the methods to retrieve the list of filenames that match a specified wildcard pattern in Go. Both methods are straightforward and can be easily implemented in your Go programs. The "filepath.Glob" method is recommended for most use cases, but the "ioutil.ReadDir" method can be useful when you need more control over the file filtering process.
- Related Articles
- Golang Program to get the list the name of files in a specified directory
- Golang program to get value from the hash collection based on specified key
- Golang Program to get the subarray from an array using a specified range of indices
- How to Get the String in Specified Base in Golang?
- C# Program to get the type of the specified Enumeration
- Wildcard Pattern Matching
- Golang Program to read the specified number of characters from the existing file
- Golang Program to Print Spiral Pattern of Numbers
- Golang Program to remove a specified directory
- Golang Program to get the magnitude of the given number
- Golang program to get the size of the hash collection
- Golang program to get the file extension
- Golang program to get the current weekday
- Golang Program to Get the size of a directory
- Golang Program To Create Pyramid And Pattern
