Skip to content

Instantly share code, notes, and snippets.

@boyter
Created August 19, 2018 09:56
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 boyter/2c0b2ed2c9908ec995ce044e5d93994a to your computer and use it in GitHub Desktop.
Save boyter/2c0b2ed2c9908ec995ce044e5d93994a to your computer and use it in GitHub Desktop.
Walk directory in Go
package main
import (
"fmt"
"github.com/karrick/godirwalk"
"io/ioutil"
)
func main() {
godirwalk.Walk("./", &godirwalk.Options{
Unsorted: true,
Callback: func(osPathname string, info *godirwalk.Dirent) error {
if !info.IsDir() {
content, err := ioutil.ReadFile(osPathname)
bytesCount := 0
if err == nil {
for index := 0; index < len(content); index++ {
if content[index] == 0 {
fmt.Println(fmt.Sprintf("./%s bytes=%d binary file", osPathname, bytesCount))
break
}
bytesCount++
}
fmt.Println(fmt.Sprintf("./%s bytes=%d", osPathname, bytesCount))
}
}
return nil
},
ErrorCallback: func(osPathname string, err error) godirwalk.ErrorAction {
return godirwalk.SkipNode
},
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment