[Golang] Walk All Files in Directory
source link: http://siongui.github.io/2016/02/04/go-walk-all-files-in-directory/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
[Golang] Walk All Files in Directory
Updated: May 10, 2018
List all files in a directory in Golang. Use filepath.Walk in Go standard library, which is similar to Python os.walk.
import ( "fmt" "os" "path/filepath" ) func WalkAllFilesInDir(dir string) error { return filepath.Walk(dir, func(path string, info os.FileInfo, e error) error { if e != nil { return e } // check if it is a regular file (not dir) if info.Mode().IsRegular() { fmt.Println("file name:", info.Name()) fmt.Println("file path:", path) } return nil }) }
If you do not want to walk into sub-directories, use ioutil.ReadDir.
Tested on: Ubuntu Linux 18.04, Go 1.10.2.
References:
[1]golang list files in directory
[2]List directory in go - Stack Overflow
Author: Siong-Ui Te ∈ Category: Go
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK