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
go语言实现遍历目录,及查找特定的文件类型的更多相关文章
- php源码之遍历目录下的所有的文件
<?php //遍历目录下的所有的文件 -- 递归调用 // http://www.manongjc.com/article/1495.html function get_all_file1($ ...
- 使用 NIO.2 遍历目录下所有的Java文件
package wellGrounded; import java.io.IOException; import java.nio.file.FileVisitResult; import java. ...
- Python 遍历目录下的子目录和文件
import os A: 遍历目录下的子目录和文件 for root,dirs ,files in os.walk(path) root:要访问的路径名 dirs:遍历目录下的子目录 files:遍历 ...
- java 20 -2 递归之找特定目录下的特定格式文件
/* 需求:把C:\Users\Administrator\Desktop\记录目录下所有以.java结尾的文件的绝对路径输出到控制台 分析: A:封装该目录 B:获取该目录下的所有文件或文件夹的Fi ...
- c++两种字符串赋值方式 并介绍 C语言下遍历目录文件的方式
c++字符串声明:一种是声明字符数组并赋值,另一种是直接声明string类 #define _CRT_SECURE_NO_WARNINGS #include<iostream> #incl ...
- C语言二叉树遍历及路径查找
#include<iostream> #include<stdio.h> #include<math.h> #include<malloc.h> usi ...
- php递归遍历目录计算其大小(文件包括目录和普通文件)
<?php function countdir($path){ $size = 0; //size = 0; 跟 size = null; 怎么结果不一样 $path = rtrim($path ...
- shell:遍历目录和子目录的所有文件
#!/bin/bash function getdir(){ ` do dir_or_file=$"/"$element if [ -d $dir_or_file ] then g ...
- shell:遍历目录和子目录的所有文件及匹配文件内容到日志
过滤文件内网 #!/bin/bash function getdir(){ ` do dir_or_file=$"/"$element if [ -d $dir_or_file ] ...
随机推荐
- hibernate学习(一)
一.准备工作:导入jar包 1.hibernate的jar包 位置: hibernate-release-5.0.2.Final\hibernate-release-5.0.2.Final\lib\r ...
- CSU 1506(最小费用最大流)
传送门:Double Shortest Paths 题意:有两个人:给出路径之间第一个人走所需要的费用和第二个人走所需要的费用(在第一个人所需的 费用上再加上第二次的费用):求两个人一共所需要的最小费 ...
- 扯谈网络编程之自己实现ping
ping是基于ICMP(Internet Control Message Protocol)协议实现的.而ICMP协议是在IP层实现的. ping实际上是发起者发送一个Echo Request(typ ...
- 《TCP/IP作品详细解释2:实现》笔记--Radix树路由表
通过IP完整的路由是路由机制,它通过搜索路由表来确定从哪个分组被发送的接口执行此,它是不一样的路由策略,路由策略 它是一组规则,这些规则可以被用来确定哪些路由编程到路由表,Net/3内核实现的路由机制 ...
- firefox同步数据时无响应问题
之前设置了firefox的数据同步,可以在不同电脑上,同步自己的书签等信息,感觉很方便实用,最近在点工具立即同步时,不报错,书签也没有同步,没有任何响应: 后来查了许多网上资料,都不见效,无意间看到 ...
- 【VB/.NET】Converting VB6 to VB.NET 【Part II】【之四】
第四部分 原文 DLLs, DAO, RDO, ADO, and AD.NET; the History of VB DBs In the early versions of VB, there we ...
- 下载jdk文件后缀是.gz而不是.tar.gz怎么办
用chrom浏览器下载了linux版的jdk,发现文件后缀是.gz,没看过这玩意,一打开,还是一个.gz文件,原本以为是新文件后缀呢.那个百度google啊. . ..最后都没发现有这方面的资料啊.. ...
- JDK源码分析—— ArrayBlockingQueue 和 LinkedBlockingQueue
JDK源码分析—— ArrayBlockingQueue 和 LinkedBlockingQueue 目的:本文通过分析JDK源码来对比ArrayBlockingQueue 和LinkedBlocki ...
- 【翻译】我钟爱的Visual Studio前端开发工具/扩展
原文:[翻译]我钟爱的Visual Studio前端开发工具/扩展 怎么样让Visual Studio更好地编写HTML5, CSS3, JavaScript, jQuery,换句话说就是如何更好地做 ...
- Android 监听SMS短信
当设备接收到一条新的SMS消息时,就会广播一个包括了android.provider.Telephony.SMS_RECEIVED动作的Intent. 注意,这个动作是一个字符串值,SDK 1.0不再 ...