Created
August 19, 2018 09:56
-
-
Save boyter/2c0b2ed2c9908ec995ce044e5d93994a to your computer and use it in GitHub Desktop.
Walk directory in Go
This file contains 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" | |
"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