golang遍历文件夹

golang遍历文件夹:

 

func main() {
    //方式一
    filepath.Walk("temp/", func (path string, info os.FileInfo, err error) error {
        fmt.Println(path)
        return nil
    })

    //方式二
    getFileList("temp/")
}

func getFileList(path string) {
    fs,_:= ioutil.ReadDir(path)
    for _,file:=range fs{
        if file.IsDir(){
            fmt.Println(path+file.Name())
            getFileList(path+file.Name()+"/")
        }else{
            fmt.Println(path+file.Name())
        }
    }
}

 

posted @ 2019-06-05 17:55  .追风逐月  阅读(8034)  评论(0编辑  收藏  举报