Golang的文件查找
获取文件基本 先上代码
type TFileInfo struct {
FileName string `json:"file_name"`
Size int64 `json:"size"`
ModTime int64 `json:"mod_time"`
}
type TSearchFile struct {
Files []TFileInfo
}
func (this *TSearchFile) Count() int {
return len(this.Files)
}
func (this *TSearchFile) Search(path string) error {
files, err := ioutil.ReadDir(path)
if err != nil {
return err
}
Os := runtime.GOOS
P := "/"
if Os == "windows" {
P = "\\"
}
for _, f := range files {
if !f.IsDir() {
data := TFileInfo{
FileName: path + P + f.Name(),
Size: f.Size(),
ModTime: f.ModTime().Unix(),
}
this.Files = append(this.Files, data)
}
}
return nil
}
文件信息 记录了 文件的
更改时间
大小
文件全路径
如果需要 扫描 子文件夹内的文件 使用 IsDir() 递归即可
还没有评论,来说两句吧...