Golang traverse the file directory

go traverse the specified directory and get all paths

var allFiles []string
func readDirFile(dir string) {
	files, err := ioutil.ReadDir(dir)
	if err != nil {
		log.Fatal(err)
	}
	for _, file := range files {
		if file.IsDir() {
			readDirFile(dir + "/" + file.Name())
		} else {
			allFiles = append(allFiles, dir+"/"+file.Name())
		}
	}
}

Leave a Reply

Your email address will not be published.

en_USEnglish