package main import ( "fmt" "os" "path/filepath" "math/rand" "time" ) func main() { root := "." if len(os.Args) > 1 { root = os.Args[1] } files := make([]string, 0, 32) err := filepath.Walk(root, func(p string, f os.FileInfo, err error) error { if err != nil { return err } if !f.IsDir() { files = append(files, p) } return nil }) if err != nil { fmt.Fprintf(os.Stderr, "** Error: %s\n", err.Error()) os.Exit(1) } rand.Seed(time.Now().UTC().UnixNano()) i := rand.Int31n(int32(len(files))) fmt.Printf("%s\n", root + "/" + files[i]) }