// 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. struts2错误验证

    在登陆的时候一般要用错误验证功能.效果如图: 在action层的写法: this.addActionError("username或password错误"); 在jsp页面上取值: ...

  2. Android获取设备採用的时间制式(12小时制式或24小时制式)

    /** * 获取设备採用的时间制式(12小时制式或者24小时制式) * 注意: * 在模拟器上获取的时间制式为空 */ private void getTime_12_24(Context conte ...

  3. MySQLdb的安装与使用

    一.安装 安装已编译版本号(此方法简便快捷): http://www.codegood.com/downloads 依据自己系统下载,双击安装,搞定 然后import MySQLdb.查看是否成功 我 ...

  4. JEECG开源团队招募新成员 2014年

    JEECG开源团队招募新成员 2014年 截止日期:2014-06-01        JEECG开源项目 是一款基于代码生成器的微云高速开发平台.提供企业高速开发和採用微信实现移动应用的解决方式.J ...

  5. Lua 解释器

    Lua 解释器 警告⚠️:这将是一个又臭又长的系列教程,教程结束的时候,你将拥有一个除了性能差劲.扩展性差.标准库不完善之外,其他方面都和官方相差无几的 Lua 语言解释器.说白了,这个系列的教程实现 ...

  6. NET之全平台一体化

    NET之全平台一体化的体验 一.前言 近来利用空闲时间研究了一下Xamarin的技术,想想既然提供了如此好的支持,就该尝试一切可能,来一个”大小通吃“. 何为全平台:APP包括Android.IOS. ...

  7. TextKit学习(四)通过boundingRectWithSize:options:attributes:context:计算文本尺寸

    之前用Text Kit写Reader的时候,在分页时要计算一段文本的尺寸大小,之前使用了NSString类的sizeWithFont:constrainedToSize:lineBreakMode:方 ...

  8. MVC应用程序与多选列表(checkbox list)

    原文:MVC应用程序与多选列表(checkbox list) 程序中,经常会使用checkbox lsit来呈现数.能让用户有多选项目.此博文Insus.NET练习的checkbox list相关各个 ...

  9. SendMessage发送自定义消息及消息响应

    控件向父窗体发送自定义消息,父窗体定义处理此消息的函数   效果描述: 指定哪个类添加自定义消息:(当然这个类必须是CmdTarget的子类,不然不能处理消息) 添加消息 实现消息函数:(wParam ...

  10. [leetcode]3 Sum closest

    问题叙述性说明: Given an array S of n integers, find three integers in S such that the sum is closest to a ...