這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。
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 string20 strRet, _ = os.Getwd()21 //ostype := os.Getenv("GOOS") // windows, linux22 23 if ostype == "windows" {24 strRet += "\\"25 } else if ostype == "linux" {26 strRet += "/"27 }28 29 if f == nil {30 return err31 }32 if f.IsDir() {33 return nil34 }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 ostype45 fmt.Println(strRet) //list the file46 47 return nil48 }49 50 func getFileList(path string) string {51 //var strRet string52 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 string72 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.sample10 E:\golang_test\FolderList\.git\hooks\post-update.sample11 E:\golang_test\FolderList\.git\hooks\pre-applypatch.sample12 E:\golang_test\FolderList\.git\hooks\pre-commit.sample13 E:\golang_test\FolderList\.git\hooks\pre-rebase.sample14 E:\golang_test\FolderList\.git\hooks\prepare-commit-msg.sample15 E:\golang_test\FolderList\.git\hooks\update.sample16 E:\golang_test\FolderList\.git\index17 E:\golang_test\FolderList\.git\info\exclude18 E:\golang_test\FolderList\.git\logs\HEAD19 E:\golang_test\FolderList\.git\logs\refs\heads\master20 E:\golang_test\FolderList\.git\logs\refs\remotes\origin\master21 E:\golang_test\FolderList\.git\objects\26\1859e5deb5e8b620e8effcdddbd76f749f89db22 E:\golang_test\FolderList\.git\objects\3d\8e579829a9d9f604604e81f008f536da785a0a23 E:\golang_test\FolderList\.git\objects\8d\362f848a2be8746747bf8377ed0db288a30fa724 E:\golang_test\FolderList\.git\objects\97\26e0fab404ab3ee9886b2e319df56326ac887425 E:\golang_test\FolderList\.git\objects\aa\a29dfb8415b1fefcfe4eddd799b0fc516c3f8a26 E:\golang_test\FolderList\.git\objects\db\cbdce2e9c70e4023594cee8e4fefe9d539493427 E:\golang_test\FolderList\.git\objects\e2\51918e7226b197e8ad32cbe30c61ddbd262f9a28 E:\golang_test\FolderList\.git\objects\ff\f5e740d47a1451f7d778de8016ebb3dd6c58dd29 E:\golang_test\FolderList\.git\refs\heads\master30 E:\golang_test\FolderList\.git\refs\remotes\origin\master31 E:\golang_test\FolderList\README.md32 E:\golang_test\FolderList\filelist.exe33 E:\golang_test\FolderList\filelist.go34 Index = 0 Value = E:\golang_test\FolderList\filelist.go