-
-
Save nextkitt/917255f39a3caa4040f78239fa707336 to your computer and use it in GitHub Desktop.
List files in a directory using 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" | |
"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) | |
} | |
} |
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
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 | |
} |
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
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) | |
} | |
} |
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
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 | |
} |
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
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 | |
} |
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 ( | |
"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