Title: Go language uses the path/filepath.Glob function to obtain the file path list and file information
Introduction:
During the development process, we often need to obtain the file path list of the specified pattern, and at the same time Get the details of the corresponding file. The path/filepath package of Go language provides the Glob function to meet this requirement. This article will introduce how to use the Glob function to list a list of file paths in a specified pattern, and return a list of file information objects and error messages at the same time.
1. Path/filepath package
The path/filepath package provided by Go language is located in the standard library. It implements some common operations on paths and file names. The Glob function can match the file path of the specified pattern and return a list of matching file paths.
2. Use the Glob function to obtain the file path list
Below we use a simple example to demonstrate how to use the Glob function to obtain the file path list. We assume that we need to obtain a list of all file paths with the suffix ".txt" in a specified directory.
The sample code is as follows:
package main import ( "fmt" "path/filepath" ) func main() { // 指定目录 dir := "./path/to/dir" // 模式匹配 pattern := filepath.Join(dir, "*.txt") // 获取匹配的文件路径列表 filePaths, err := filepath.Glob(pattern) if err != nil { fmt.Println("获取文件路径列表失败:", err) return } // 打印文件路径列表 fmt.Println("文件路径列表:") for _, filePath := range filePaths { fmt.Println(filePath) } }
In the above code, first we specify a directory dir
, and then use the filepath.Join
function to join the directory path Spliced together with the pattern to generate the matching pattern pattern
. Next, we call the filepath.Glob
function and pass in the pattern pattern
to get the file path list filePaths
. Finally, we print out all the file paths by iterating through the list.
3. Return the file information object list and error message
While obtaining the file path list, we can also obtain the detailed information of each file. Obtain the file information of the specified path by calling the os.Stat
function. Next, we improve the above example code to obtain the file information object list and error information.
The sample code is as follows:
package main import ( "fmt" "os" "path/filepath" ) func main() { // 指定目录 dir := "./path/to/dir" // 模式匹配 pattern := filepath.Join(dir, "*.txt") // 获取匹配的文件路径列表 filePaths, err := filepath.Glob(pattern) if err != nil { fmt.Println("获取文件路径列表失败:", err) return } // 获取文件信息列表 var fileInfos []os.FileInfo for _, filePath := range filePaths { fileInfo, err := os.Stat(filePath) if err != nil { fmt.Println("获取文件信息失败:", err) continue } fileInfos = append(fileInfos, fileInfo) } // 打印文件路径及信息 fmt.Println("文件路径及信息:") for i, filePath := range filePaths { fileInfo := fileInfos[i] fmt.Println("文件路径:", filePath) fmt.Println("文件大小:", fileInfo.Size()) fmt.Println("文件修改时间:", fileInfo.ModTime()) fmt.Println("文件权限:", fileInfo.Mode().String()) } }
In the above code, while obtaining the file path list, we loop through each file path, and then call the os.Stat
function to obtain Details of the corresponding file. Save information for each file in the fileInfos
list. Finally, we print out the file path and details by traversing the list.
End:
This article introduces how to use the Glob function in the path/filepath package of Go language to obtain the file path list of the specified pattern, and return the file information object list and error message at the same time. Through the explanation of code examples, readers can better understand and use this function to meet their own development needs. Hope this article is helpful to readers.
The above is the detailed content of Use the path/filepath.Glob function to list the file path list of the specified mode and return the file information object list and error message. For more information, please follow other related articles on the PHP Chinese website!