Forked from francoishill/loop_files_folders_recursively.go
Last active
January 2, 2023 10:31
-
-
Save fubarhouse/5ae3fdd5ed5be9e718a92d9b3c780a22 to your computer and use it in GitHub Desktop.
Loop through files and folders recursively in golang
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"os" | |
"path/filepath" | |
) | |
func run() ([]string, error) { | |
searchDir := "c:/path/to/dir" | |
fileList := make([]string, 0) | |
e := filepath.Walk(searchDir, func(path string, f os.FileInfo, err error) error { | |
fileList = append(fileList, path) | |
return err | |
}) | |
if e != nil { | |
panic(e) | |
} | |
for _, file := range fileList { | |
fmt.Println(file) | |
} | |
return fileList, nil | |
} | |
func main() { | |
run() | |
} |
it was giving me errors so I used ioutil.ReadDir
instead and it worked: https://flaviocopes.com/go-list-files/#using-ioutil-readdir
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks! 😊