Home >Backend Development >Golang >Use the path/filepath.Glob function to list a list of file paths in the specified pattern and return a list of file information objects
Title: Use the path/filepath.Glob function to list the file path list of the specified pattern and return the file information object list
In Go language, we can use path/filepath
The Glob
function in the package lists the path list of the specified pattern file and returns the file information object list. This is very useful when you need to process specific types of files in a certain directory. This article will introduce how to use the Glob
function and provide corresponding code examples.
Glob
The function searches for matching files in the file system through the specified pattern and returns a list of file paths that meet the conditions. When searching for files, wildcards can be used in the pattern to match file names. For example: *
means matching any number of any characters, ?
means matching one arbitrary character, [abc]
means matching the characters a
, b
or c
, etc. In addition, you can also use **
to indicate matching subdirectories at any level.
The following is a simple code example that demonstrates how to use the Glob
function to list all file paths in a directory with the suffix .txt
. and returns a list of file information objects.
package main import ( "fmt" "os" "path/filepath" ) func main() { files, err := filepath.Glob("dir/*.txt") if err != nil { fmt.Println("无法读取文件路径:", err) os.Exit(1) } var fileInfos []os.FileInfo for _, file := range files { fileInfo, err := os.Stat(file) if err != nil { fmt.Println("无法读取文件信息:", err) os.Exit(1) } fileInfos = append(fileInfos, fileInfo) } fmt.Println("文件路径列表:") for _, file := range files { fmt.Println(file) } fmt.Println("文件信息列表:") for _, fileInfo := range fileInfos { fmt.Println(fileInfo.Name(), fileInfo.Size(), fileInfo.Mode(), fileInfo.ModTime()) } }
In the above example, first use the Glob
function to obtain a list of all file paths that satisfy the specified pattern. The pattern is dir/*.txt
, which means matching All files in the dir
directory with the suffix .txt
. Then, use the os.Stat
function to obtain the file information object for each file and store it in the fileInfos
list. Finally, print the file path list and file information list respectively.
Through the above example, we can learn how to use the Glob
function to easily list the file paths that meet the specified pattern, and obtain the file through the os.Stat
function information. This is very helpful for operations such as batch processing or statistical information on specific types of files.
To summarize, this article introduces the Glob
function in the path/filepath
package, and demonstrates through code examples how to use this function to list files in a specified pattern. A list of paths and returns a list of file information objects. I hope this article can be helpful to scenarios where you need to process file paths and file information in Go language development.
The above is the detailed content of Use the path/filepath.Glob function to list a list of file paths in the specified pattern and return a list of file information objects. For more information, please follow other related articles on the PHP Chinese website!