Skip to content

Instantly share code, notes, and snippets.

@xeoncross
Forked from manigandand/list_directory.go
Created February 8, 2019 15:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xeoncross/44e30ee37675911b1c28550ace4ec537 to your computer and use it in GitHub Desktop.
Save xeoncross/44e30ee37675911b1c28550ace4ec537 to your computer and use it in GitHub Desktop.
List files in a directory using golang
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
)
func main() {
var (
root string
files []string
err error
)
if len(os.Args) == 1 {
log.Fatal("No path given, Please specify path.")
return
}
if root = os.Args[1]; root == "" {
log.Fatal("No path given, Please specify path.")
return
}
// filepath.Walk
files, err = FilePathWalkDir(root)
if err != nil {
panic(err)
}
// ioutil.ReadDir
files, err = IOReadDir(root)
if err != nil {
panic(err)
}
//os.File.Readdir
files, err = OSReadDir(root)
if err != nil {
panic(err)
}
for _, file := range files {
fmt.Println(file)
}
}
func FilePathWalkDir(root string) ([]string, error) {
var files []string
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if !info.IsDir() {
files = append(files, path)
}
return nil
})
return files, err
}
func FpW(n int, root string) {
for i := 0; i < n; i++ {
FilePathWalkDir(root)
}
}
func IoRdir(n int, root string) {
for i := 0; i < n; i++ {
IOReadDir(root)
}
}
func OsRdir(n int, root string) {
for i := 0; i < n; i++ {
OSReadDir(root)
}
}
func IOReadDir(root string) ([]string, error) {
var files []string
fileInfo, err := ioutil.ReadDir(root)
if err != nil {
return files, err
}
for _, file := range fileInfo {
files = append(files, file.Name())
}
return files, nil
}
func OSReadDir(root string) ([]string, error) {
var files []string
f, err := os.Open(root)
if err != nil {
return files, err
}
fileInfo, err := f.Readdir(-1)
f.Close()
if err != nil {
return files, err
}
for _, file := range fileInfo {
files = append(files, file.Name())
}
return files, nil
}
package main
import (
"testing"
)
var root string = "/home/manigandan/Desktop/Manigandan/sample"
func BenchmarkFilePathWalkDir1(b *testing.B) { benchmarkFilePathWalkDir(1, root, b) }
func BenchmarkFilePathWalkDir2(b *testing.B) { benchmarkFilePathWalkDir(2, root, b) }
func BenchmarkFilePathWalkDir3(b *testing.B) { benchmarkFilePathWalkDir(3, root, b) }
func BenchmarkFilePathWalkDir10(b *testing.B) { benchmarkFilePathWalkDir(10, root, b) }
func BenchmarkFilePathWalkDir20(b *testing.B) { benchmarkFilePathWalkDir(20, root, b) }
func BenchmarkFilePathWalkDir30(b *testing.B) { benchmarkFilePathWalkDir(30, root, b) }
func BenchmarkFilePathWalkDir40(b *testing.B) { benchmarkFilePathWalkDir(40, root, b) }
func BenchmarkFilePathWalkDir50(b *testing.B) { benchmarkFilePathWalkDir(50, root, b) }
func BenchmarkIOReadDir1(b *testing.B) { benchmarkIOReadDir(1, root, b) }
func BenchmarkIOReadDir2(b *testing.B) { benchmarkIOReadDir(2, root, b) }
func BenchmarkIOReadDir3(b *testing.B) { benchmarkIOReadDir(3, root, b) }
func BenchmarkIOReadDir10(b *testing.B) { benchmarkIOReadDir(10, root, b) }
func BenchmarkIOReadDir20(b *testing.B) { benchmarkIOReadDir(20, root, b) }
func BenchmarkIOReadDir30(b *testing.B) { benchmarkIOReadDir(30, root, b) }
func BenchmarkIOReadDir40(b *testing.B) { benchmarkIOReadDir(40, root, b) }
func BenchmarkIOReadDir50(b *testing.B) { benchmarkIOReadDir(50, root, b) }
func BenchmarkOSReadDir1(b *testing.B) { benchmarkOSReadDir(1, root, b) }
func BenchmarkOSReadDir2(b *testing.B) { benchmarkOSReadDir(2, root, b) }
func BenchmarkOSReadDir3(b *testing.B) { benchmarkOSReadDir(3, root, b) }
func BenchmarkOSReadDir10(b *testing.B) { benchmarkOSReadDir(10, root, b) }
func BenchmarkOSReadDir20(b *testing.B) { benchmarkOSReadDir(20, root, b) }
func BenchmarkOSReadDir30(b *testing.B) { benchmarkOSReadDir(30, root, b) }
func BenchmarkOSReadDir40(b *testing.B) { benchmarkOSReadDir(40, root, b) }
func BenchmarkOSReadDir50(b *testing.B) { benchmarkOSReadDir(50, root, b) }
func benchmarkFilePathWalkDir(i int, root string, b *testing.B) {
// run the FilePathWalkDir function b.N times
for n := 0; n < b.N; n++ {
FpW(i, root)
}
}
func benchmarkIOReadDir(i int, root string, b *testing.B) {
// run the IOReadDir function b.N times
for n := 0; n < b.N; n++ {
IoRdir(i, root)
}
}
func benchmarkOSReadDir(i int, root string, b *testing.B) {
// run the OSReadDir function b.N times
for n := 0; n < b.N; n++ {
OsRdir(i, root)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment