作者:ho世英雄 | 来源:互联网 | 2024-10-17 08:40
123456789101112131415161718192021222324252627282930313233343536373839404142434445package mainimport
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| package main
import (
"fmt"
"os"
"path/filepath"
)
var Files []string
func main() {
/*
wc, _ := os.Getwd()
var directory = flag.String("dir", wc, "which dir you wanna scan")
flag.Parse()
*/
err := Scan("/usr/local/var/www/app")
if err == nil {
fmt.Println("files:")
fmt.Sprintf("%v\n", Files)
} else {
fmt.Println("errors:")
fmt.Fprintf(os.Stderr, "%v", err)
}
}
func Scan(path string) error {
err := filepath.Walk(path, func (path string, f os.FileInfo, err error) error {
if f == nil {
return err
}
if f.IsDir() {
return nil
}
Files = append(Files, path)
return nil
})
return err
} |
上述代码并不能将path push到Files全局变量里面,我该怎么办?