Go language Implementation traverse directory, and find specific file types

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.
1 //Filelist.go2 Package Main3 4 Import (5     //"Flag"6     "FMT"7     "OS"8     "Path/filepath"9     "Strings"Ten ) One  A var ( -OSType = OS. Getenv ("GOOS")//Get system Type - ) the  - varListFile []string //get a list of files -  -Func Listfunc (pathstring, F OS. FileInfo, err Error) error { +     varStrretstring -Strret, _ =OS. GETWD () +     //OSType: = os. Getenv ("GOOS")//windows, Linux A  at     ifOSType = ="Windows" { -Strret + ="\\" -}Else ifOSType = ="Linux" { -Strret + ="/" -     } -  in     iff = =Nil { -         returnErr to     } +     ifF.isdir () { -         returnNil the     } *  $strret + = Path//+ "\ r \ n"Panax Notoginseng  -     //with strings. Hassuffix (src, suffix)//determine if the src contains suffix endings theOK: = strings. Hassuffix (Strret,". Go") +     ifOK { A  theListFile = Append (ListFile, strret)//Push directory to listfile []string +     } -     //FMT. Println (OSType)//Print OSType $Fmt. Println (strret)//List the file $  -     returnNil - } the  -Func getfilelist (pathstring)string {Wuyi     //var strret string theERR: = FilePath. Walk (Path, Listfunc)// -  Wu     ifErr! =Nil { -Fmt. Printf ("filepath. Walk () returned%v\n", Err) About     } $  -     return " " - } -  AFunc Listfilefunc (P []string) { +      forIndex, Value: =Range P { theFmt. Println ("Index =", Index,"Value =", value) -     } $ } the  the Func Main () { the     //flag. Parse () the     //root: = flag. ARG (0) -     //FMT. Println () in     varListpathstring theFmt. SCANF ("%s", &Listpath) the getfilelist (Listpath) About Listfilefunc (listfile) the     //getfilelist (Root) the  the}

The results are as follows:

1administrator@win7-20131114us/cygdrive/e/golang_test/Folderlist2$ ./filelist3 .4 E:\golang_test\FolderList\.git\COMMIT_EDITMSG5 E:\golang_test\FolderList\.git\HEAD6 E:\golang_test\FolderList\.git\config7 E:\golang_test\FolderList\.git\description8E:\golang_test\FolderList\.git\hooks\applypatch-Msg.sample9E:\golang_test\FolderList\.git\hooks\commit-Msg.sampleTenE:\golang_test\FolderList\.git\hooks\post-Update.sample OneE:\golang_test\FolderList\.git\hooks\pre-Applypatch.sample AE:\golang_test\FolderList\.git\hooks\pre-Commit.sample -E:\golang_test\FolderList\.git\hooks\pre-Rebase.sample -E:\golang_test\FolderList\.git\hooks\prepare-commit-Msg.sample the E:\golang_test\FolderList\.git\hooks\update.sample - E:\golang_test\FolderList\.git\index -E:\golang_test\FolderList\.git\Info\exclude - E:\golang_test\FolderList\.git\logs\HEAD + E:\golang_test\FolderList\.git\logs\refs\heads\master - E:\golang_test\FolderList\.git\logs\refs\remotes\origin\master +E:\golang_test\FolderList\.git\objects\ -\1859e5deb5e8b620e8effcdddbd76f749f89db A E:\golang_test\FolderList\.git\objects\3d\8e579829a9d9f604604e81f008f536da785a0a at E:\golang_test\FolderList\.git\objects\8d\362f848a2be8746747bf8377ed0db288a30fa7 -E:\golang_test\FolderList\.git\objects\ the\26e0fab404ab3ee9886b2e319df56326ac8874 - E:\golang_test\FolderList\.git\objects\aa\a29dfb8415b1fefcfe4eddd799b0fc516c3f8a - E:\golang_test\FolderList\.git\objects\db\cbdce2e9c70e4023594cee8e4fefe9d5394934 - E:\golang_test\FolderList\.git\objects\e2\51918e7226b197e8ad32cbe30c61ddbd262f9a - E:\GOLANG_TEST\FOLDERLIST\.GIT\OBJECTS\FF\F5E740D47A1451F7D778DE8016EBB3DD6C58DD in E:\golang_test\FolderList\.git\refs\heads\master - E:\golang_test\FolderList\.git\refs\remotes\origin\master to E:\golang_test\FolderList\README.md + E:\golang_test\FolderList\filelist.exe - E:\golang_test\FolderList\filelist.go theIndex =0Value = E:\golang_test\FolderList\filelist.go

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.