函数封装
type FileInfo struct {
Name string // 文件名
Size int64 // 文件大小
Path string // 文件路径
}
// GetFileList 递归获取指定目录下的所有文件
func GetFileList(path string, fileList *[]FileInfo) {
files, _ := ioutil.ReadDir(path)
for _, file := range files {
if file.IsDir() {
GetFileList(path+file.Name()+`\`, fileList) // 递归调用
} else {
*fileList = append(*fileList, FileInfo{
Name: file.Name(),
Size: file.Size(),
Path: path + file.Name(),
})
}
}
}
调用方法
var fileList []FileInfo
GetFileList(path, &fileList)
fmt.Println("文件数量:", len(fileList))
// 打印文件信息
for _, file := range fileList {
fmt.Println("file:", file.Name, file.Size, file.Path)
}