go语言实现遍历目录,及查找特定的文件类型

 1 // filelist.go
 2 package main
 3 
 4 import (
 5     //"flag"
 6     "fmt"
 7     "os"
 8     "path/filepath"
 9     "strings"
10 )
11 
12 var (
13     ostype = os.Getenv("GOOS") // 获取系统类型
14 )
15 
16 var listfile []string //获取文件列表
17 
18 func Listfunc(path string, f os.FileInfo, err error) error {
19     var strRet string
20     strRet, _ = os.Getwd()
21     //ostype := os.Getenv("GOOS") // windows, linux
22 
23     if ostype == "windows" {
24         strRet += "\\"
25     } else if ostype == "linux" {
26         strRet += "/"
27     }
28 
29     if f == nil {
30         return err
31     }
32     if f.IsDir() {
33         return nil
34     }
35 
36     strRet += path //+ "\r\n"
37 
38     //用strings.HasSuffix(src, suffix)//判断src中是否包含 suffix结尾
39     ok := strings.HasSuffix(strRet, ".go")
40     if ok {
41 
42         listfile = append(listfile, strRet) //将目录push到listfile []string中
43     }
44     //fmt.Println(ostype) // print ostype
45     fmt.Println(strRet) //list the file
46 
47     return nil
48 }
49 
50 func getFileList(path string) string {
51     //var strRet string
52     err := filepath.Walk(path, Listfunc) //
53 
54     if err != nil {
55         fmt.Printf("filepath.Walk() returned %v\n", err)
56     }
57 
58     return " "
59 }
60 
61 func ListFileFunc(p []string) {
62     for index, value := range p {
63         fmt.Println("Index = ", index, "Value = ", value)
64     }
65 }
66 
67 func main() {
68     //flag.Parse()
69     //root := flag.Arg(0)
70     //fmt.Println()
71     var listpath string
72     fmt.Scanf("%s", &listpath)
73     getFileList(listpath)
74     ListFileFunc(listfile)
75     //getFileList(root)
76 
77 }

运行效果如下:

 1 Administrator@WIN7-20131114US /cygdrive/e/golang_test/FolderList
 2 $ ./filelist
 3 .
 4 E:\golang_test\FolderList\.git\COMMIT_EDITMSG
 5 E:\golang_test\FolderList\.git\HEAD
 6 E:\golang_test\FolderList\.git\config
 7 E:\golang_test\FolderList\.git\description
 8 E:\golang_test\FolderList\.git\hooks\applypatch-msg.sample
 9 E:\golang_test\FolderList\.git\hooks\commit-msg.sample
10 E:\golang_test\FolderList\.git\hooks\post-update.sample
11 E:\golang_test\FolderList\.git\hooks\pre-applypatch.sample
12 E:\golang_test\FolderList\.git\hooks\pre-commit.sample
13 E:\golang_test\FolderList\.git\hooks\pre-rebase.sample
14 E:\golang_test\FolderList\.git\hooks\prepare-commit-msg.sample
15 E:\golang_test\FolderList\.git\hooks\update.sample
16 E:\golang_test\FolderList\.git\index
17 E:\golang_test\FolderList\.git\info\exclude
18 E:\golang_test\FolderList\.git\logs\HEAD
19 E:\golang_test\FolderList\.git\logs\refs\heads\master
20 E:\golang_test\FolderList\.git\logs\refs\remotes\origin\master
21 E:\golang_test\FolderList\.git\objects\26\1859e5deb5e8b620e8effcdddbd76f749f89db
22 E:\golang_test\FolderList\.git\objects\3d\8e579829a9d9f604604e81f008f536da785a0a
23 E:\golang_test\FolderList\.git\objects\8d\362f848a2be8746747bf8377ed0db288a30fa7
24 E:\golang_test\FolderList\.git\objects\97\26e0fab404ab3ee9886b2e319df56326ac8874
25 E:\golang_test\FolderList\.git\objects\aa\a29dfb8415b1fefcfe4eddd799b0fc516c3f8a
26 E:\golang_test\FolderList\.git\objects\db\cbdce2e9c70e4023594cee8e4fefe9d5394934
27 E:\golang_test\FolderList\.git\objects\e2\51918e7226b197e8ad32cbe30c61ddbd262f9a
28 E:\golang_test\FolderList\.git\objects\ff\f5e740d47a1451f7d778de8016ebb3dd6c58dd
29 E:\golang_test\FolderList\.git\refs\heads\master
30 E:\golang_test\FolderList\.git\refs\remotes\origin\master
31 E:\golang_test\FolderList\README.md
32 E:\golang_test\FolderList\filelist.exe
33 E:\golang_test\FolderList\filelist.go
34 Index =  0 Value =  E:\golang_test\FolderList\filelist.go

 

posted @ 2014-05-23 16:48 sndnvaps 阅读(...) 评论(...) 编辑 收藏