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

 // filelist.go
package main import (
//"flag"
"fmt"
"os"
"path/filepath"
"strings"
) var (
ostype = os.Getenv("GOOS") // 获取系统类型
) var listfile []string //获取文件列表 func Listfunc(path string, f os.FileInfo, err error) error {
var strRet string
strRet, _ = os.Getwd()
//ostype := os.Getenv("GOOS") // windows, linux if ostype == "windows" {
strRet += "\\"
} else if ostype == "linux" {
strRet += "/"
} if f == nil {
return err
}
if f.IsDir() {
return nil
} strRet += path //+ "\r\n" //用strings.HasSuffix(src, suffix)//判断src中是否包含 suffix结尾
ok := strings.HasSuffix(strRet, ".go")
if ok { listfile = append(listfile, strRet) //将目录push到listfile []string中
}
//fmt.Println(ostype) // print ostype
fmt.Println(strRet) //list the file return nil
} func getFileList(path string) string {
//var strRet string
err := filepath.Walk(path, Listfunc) // if err != nil {
fmt.Printf("filepath.Walk() returned %v\n", err)
} return " "
} func ListFileFunc(p []string) {
for index, value := range p {
fmt.Println("Index = ", index, "Value = ", value)
}
} func main() {
//flag.Parse()
//root := flag.Arg(0)
//fmt.Println()
var listpath string
fmt.Scanf("%s", &listpath)
getFileList(listpath)
ListFileFunc(listfile)
//getFileList(root) }

运行效果如下:

 Administrator@WIN7-20131114US /cygdrive/e/golang_test/FolderList
$ ./filelist
.
E:\golang_test\FolderList\.git\COMMIT_EDITMSG
E:\golang_test\FolderList\.git\HEAD
E:\golang_test\FolderList\.git\config
E:\golang_test\FolderList\.git\description
E:\golang_test\FolderList\.git\hooks\applypatch-msg.sample
E:\golang_test\FolderList\.git\hooks\commit-msg.sample
E:\golang_test\FolderList\.git\hooks\post-update.sample
E:\golang_test\FolderList\.git\hooks\pre-applypatch.sample
E:\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
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
E:\golang_test\FolderList\.git\objects\3d\8e579829a9d9f604604e81f008f536da785a0a
E:\golang_test\FolderList\.git\objects\8d\362f848a2be8746747bf8377ed0db288a30fa7
E:\golang_test\FolderList\.git\objects\\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
E:\golang_test\FolderList\.git\refs\heads\master
E:\golang_test\FolderList\.git\refs\remotes\origin\master
E:\golang_test\FolderList\README.md
E:\golang_test\FolderList\filelist.exe
E:\golang_test\FolderList\filelist.go
Index = Value = E:\golang_test\FolderList\filelist.go
上一篇:Python——day11 函数(对象、名称空间、作用域、嵌套、闭包)


下一篇:使用Jenkins+Calabash+Cocoapods搭建iOS持续集成环境