// 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

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

  1. php源码之遍历目录下的所有的文件

    <?php //遍历目录下的所有的文件 -- 递归调用 // http://www.manongjc.com/article/1495.html function get_all_file1($ ...

  2. 使用 NIO.2 遍历目录下所有的Java文件

    package wellGrounded; import java.io.IOException; import java.nio.file.FileVisitResult; import java. ...

  3. Python 遍历目录下的子目录和文件

    import os A: 遍历目录下的子目录和文件 for root,dirs ,files in os.walk(path) root:要访问的路径名 dirs:遍历目录下的子目录 files:遍历 ...

  4. java 20 -2 递归之找特定目录下的特定格式文件

    /* 需求:把C:\Users\Administrator\Desktop\记录目录下所有以.java结尾的文件的绝对路径输出到控制台 分析: A:封装该目录 B:获取该目录下的所有文件或文件夹的Fi ...

  5. c++两种字符串赋值方式 并介绍 C语言下遍历目录文件的方式

    c++字符串声明:一种是声明字符数组并赋值,另一种是直接声明string类 #define _CRT_SECURE_NO_WARNINGS #include<iostream> #incl ...

  6. C语言二叉树遍历及路径查找

    #include<iostream> #include<stdio.h> #include<math.h> #include<malloc.h> usi ...

  7. php递归遍历目录计算其大小(文件包括目录和普通文件)

    <?php function countdir($path){ $size = 0; //size = 0; 跟 size = null; 怎么结果不一样 $path = rtrim($path ...

  8. shell:遍历目录和子目录的所有文件

    #!/bin/bash function getdir(){ ` do dir_or_file=$"/"$element if [ -d $dir_or_file ] then g ...

  9. shell:遍历目录和子目录的所有文件及匹配文件内容到日志

    过滤文件内网 #!/bin/bash function getdir(){ ` do dir_or_file=$"/"$element if [ -d $dir_or_file ] ...

随机推荐

  1. thinkPHP框架介绍(一)

    原文:thinkPHP框架介绍(一) 一.ThinkPHP的介绍 期间有对ThinkPHP框架在学习上的问题欢迎大家交流:QQ:812231134 MVC M - Model 模型           ...

  2. java学习笔记12--国际化

    java学习笔记12--国际化 国际化的操作就是指一个程序可以同时适应多门语言,即:如果现在程序者是中国人,则会以中文为显示文字,如果现在程序的使用者是英国人,则会以英语为显示的文字,也就是说可以通过 ...

  3. 利用Node.js实现模拟Session验证的登陆

    1.身份验证和用户登陆 在一般的Web应用上,假设要实现用户登陆,最经常使用,也是最简单的方法就是使用Session,主要的思路是在Session中保留一些用户身份信息,然后每次在Session中取, ...

  4. &lt;xliff:g&gt;标签

    摘要: 这是Android4.3Mms源代码中的strings.xml的一段代码: <!--Settings item desciption for integer auto-delete sm ...

  5. PHP SPL他们留下的宝石

    Rafael Dohms 上面的篇文章 让我为之惊艳,忍不住就翻译了下来,同一时候补充了部分内容. SPL,PHP 标准库(Standard PHP Library) ,此从 PHP 5.0 起内置的 ...

  6. java自己主动打开包装盒很容易导致两个误区

    从J2SE 5.0开始提供基本数据类型的自己主动装箱(autoboxing).拆箱(unboxing)功能. 何为自己主动装箱: 当我们创建一个Integer对象时,却能够这样: Integer i ...

  7. Kafka 协议实现中的内存优化

    Kafka 协议实现中的内存优化 Kafka 协议实现中的内存优化   Jusfr 原创,转载请注明来自博客园 Request 与 Response 的响应格式 Request 与 Response ...

  8. jquery ui中 accordion的问题及我的解决方法

    原文:jquery ui中 accordion的问题及我的解决方法 jquery有一套所谓的ui组件,很不错的.如果有兴趣的朋友,可以参考http://jqueryui.com/ 但其中的accord ...

  9. uboot启动阶段修改启动参数方法及分析

    作者:围补 本来启动方式这节不是什么复杂的事儿,不过想简单的说清楚明白,还真是不知道怎么组织.毕竟文字跟有声语言表达有别.但愿简单的东西别让我讲的太复杂! Arm板系统文件一般有三个——bootloa ...

  10. hdu3974(线段树+dfs)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3974 题意:给定点的上下级关系,规定如果给i分配任务a,那么他的所有下属.都停下手上的工作,开始做a. ...