go語言實現遍曆目錄,及尋找特定的檔案類型

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。
 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

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.