当前位置:网站首页>Go recursively loops through folders

Go recursively loops through folders

2022-04-23 19:21:00 Handsome that handsome

/** dirname  Directory name  level  Hierarchy  */
func listFiles(dirName string,level int)  {
    
	//level Used to record the current recursive hierarchy , Generate spaces with a sense of hierarchy 
	s :="|--"
	for i:=0;i<level;i++ {
    
		s = "| "+s
	}
	fileInfos,err := ioutil.ReadDir(dirName)
	if err != nil {
    
		log.Fatal(err)
	}
	for _,fi := range fileInfos{
    
		filename := dirName+"/"+fi.Name()
		fmt.Printf("%s%s\n",s,filename)
		if fi.IsDir() {
    
			// Recursive call method 
			listFiles(filename,level+1)
		}
	}
}

版权声明
本文为[Handsome that handsome]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210557451118.html