Skip to content

Instantly share code, notes, and snippets.

@harshavardhana
Created October 15, 2013 22:54
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 harshavardhana/6999923 to your computer and use it in GitHub Desktop.
Save harshavardhana/6999923 to your computer and use it in GitHub Desktop.
Filewalk
package main
import (
"path/filepath"
"os"
"flag"
"fmt"
)
type fileattr struct {
fileinfo os.FileInfo
path string
}
var filelist []fileattr
func Append(slice []fileattr, data fileattr) []fileattr {
l := len(slice)
if data.path != "" {
newSlice := make([]fileattr, l+1)
copy(newSlice, slice)
slice = newSlice
slice[l] = data
}
return slice
}
func filevisit(path string, f os.FileInfo, _ error) error {
var attr fileattr
attr.path = path
attr.fileinfo = f
if (attr.fileinfo.IsDir()) {
return nil
}
filelist = Append(filelist, attr)
return nil
}
func main() {
flag.Parse()
dir := flag.Arg(0)
if flag.NArg() < 1 {
fmt.Printf("Please provide arguments in this fashion <dir_checksum>\n")
os.Exit(255)
} else if flag.NArg() > 1 {
fmt.Printf("Please provide arguments in this fashion <dir_checksum>\n")
os.Exit(255)
}
filepath.Walk(dir, filevisit)
}
@anandaverma
Copy link

nice :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment