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 ] ...
随机推荐
- About VirtualBoxImages.com
About VirtualBoxImages.com | VirtualBoxImages.com About VirtualBoxImages.com About: VirtualBoxImages ...
- Android常用开源项目
Android开源项目第一篇——个性化控件(View)篇 包括ListView.ActionBar.Menu.ViewPager.Gallery.GridView.ImageView.Progre ...
- 移动App測试实战:顶级互联网企业软件測试和质量提升最佳实践
这篇是计算机类的优质预售推荐>>>><移动App測试实战:顶级互联网企业软件測试和质量提升最佳实践> 国内顶级互联网公司測试实战经验总结.阿里.腾讯.京东.携程.百 ...
- POJ1611 The Suspects (并查集)
本文出自:http://blog.csdn.net/svitter 题意:0号学生染病,有n个学生,m个小组.和0号学生同组的学生染病,病能够传染. 输入格式:n,m 数量 学生编号1,2,3,4 ...
- linux根据部署jenkins
1. Jenkins 下载 Jenkins 下载网址:http://jenkins-ci.org/ 2. Jenkins 安装 (1) 安装JDK JDK下载:http://www.oracle.co ...
- jQuery简单过滤选择器
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <!--jQuery选择器详解 根据所获 ...
- Java_并发线程_CompletionService
1.CompletionService源代码分析 CompletionService内部实现还是维护了一个可堵塞的队列,通过代理设计模式.从而操作队列. /** * Creates an Execut ...
- MVC之Session State性能
ASP.NET MVC之Session State性能问题(七) 前言 这一节翻译一篇有关Session State性能问题的文章,非一字一句翻译. 话题 不知道我们在真实环境中是否用到了Sess ...
- jdk1.6与1.7垃圾回收
最近项目中遇到了个关于JVM中GC线程数的问题,做一下简单的总结 问题场景: server:均为 sun公司的solaris 系统 CPU 128个 项目8.1时使用的 java版本: jdk1. ...
- 嗨翻C语言
<嗨翻C语言> 基本信息 作者: (美)David Griffiths Dawn Griffiths 译者: 程亦超 出版社:人民邮电出版社 ISBN:978711531884 ...