zoukankan      html  css  js  c++  java
  • 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 //+ "
    "
    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
    ", 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_testFolderList.gitCOMMIT_EDITMSG
     5 E:golang_testFolderList.gitHEAD
     6 E:golang_testFolderList.gitconfig
     7 E:golang_testFolderList.gitdescription
     8 E:golang_testFolderList.githooksapplypatch-msg.sample
     9 E:golang_testFolderList.githookscommit-msg.sample
    10 E:golang_testFolderList.githookspost-update.sample
    11 E:golang_testFolderList.githookspre-applypatch.sample
    12 E:golang_testFolderList.githookspre-commit.sample
    13 E:golang_testFolderList.githookspre-rebase.sample
    14 E:golang_testFolderList.githooksprepare-commit-msg.sample
    15 E:golang_testFolderList.githooksupdate.sample
    16 E:golang_testFolderList.gitindex
    17 E:golang_testFolderList.gitinfoexclude
    18 E:golang_testFolderList.gitlogsHEAD
    19 E:golang_testFolderList.gitlogs
    efsheadsmaster
    20 E:golang_testFolderList.gitlogs
    efs
    emotesoriginmaster
    21 E:golang_testFolderList.gitobjects261859e5deb5e8b620e8effcdddbd76f749f89db
    22 E:golang_testFolderList.gitobjects3d8e579829a9d9f604604e81f008f536da785a0a
    23 E:golang_testFolderList.gitobjects8d362f848a2be8746747bf8377ed0db288a30fa7
    24 E:golang_testFolderList.gitobjects9726e0fab404ab3ee9886b2e319df56326ac8874
    25 E:golang_testFolderList.gitobjectsaaa29dfb8415b1fefcfe4eddd799b0fc516c3f8a
    26 E:golang_testFolderList.gitobjectsdbcbdce2e9c70e4023594cee8e4fefe9d5394934
    27 E:golang_testFolderList.gitobjectse251918e7226b197e8ad32cbe30c61ddbd262f9a
    28 E:golang_testFolderList.gitobjectsfff5e740d47a1451f7d778de8016ebb3dd6c58dd
    29 E:golang_testFolderList.git
    efsheadsmaster
    30 E:golang_testFolderList.git
    efs
    emotesoriginmaster
    31 E:golang_testFolderListREADME.md
    32 E:golang_testFolderListfilelist.exe
    33 E:golang_testFolderListfilelist.go
    34 Index =  0 Value =  E:golang_testFolderListfilelist.go
  • 相关阅读:
    PostgreSQL备份工具-pg_probackup
    Multi-Master Replication Solutions for PostgreSQL
    PostgreSQL高可用:多主复制解决方案
    Postgressql高可用(pgpool+异步流复制)转
    AudioFlinger
    GPIO口配置为上拉,下拉输入
    转【Qualcomm高通音频】音效调试_录音文件播放有杂音,如何定位原因?
    转【Qualcomm高通音频】如何使用QXDM、QCAT、CoolEditor音频日志抓取、解析和分析?
    转【Qualcomm高通音频】调试工具QACT_如何新增一套音效
    转【Qualcomm高通音频】调试工具QACT_如何更换音效的音频拓扑
  • 原文地址:https://www.cnblogs.com/sn-dnv-aps/p/3748065.html
Copyright © 2011-2022 走看看