惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

IT之家
IT之家
腾讯CDC
博客园 - Franky
S
SegmentFault 最新的问题
美团技术团队
阮一峰的网络日志
阮一峰的网络日志
J
Java Code Geeks
Y
Y Combinator Blog
Engineering at Meta
Engineering at Meta
Microsoft Security Blog
Microsoft Security Blog
MongoDB | Blog
MongoDB | Blog
I
InfoQ
T
Tailwind CSS Blog
Hugging Face - Blog
Hugging Face - Blog
B
Blog RSS Feed
博客园 - 叶小钗
博客园_首页
有赞技术团队
有赞技术团队
雷峰网
雷峰网
量子位
小众软件
小众软件
月光博客
月光博客
U
Unit 42
D
DataBreaches.Net

博客园 - 程序员老刘

SaaS多租户改造Spring项目核心代码 windows11的vmware启动报错 MySQL8的root帐号授权 centrifugal 的IM消息服务历史消息报错 支持restart的启动脚本 golang strings.Split的疑问 golang的极简流式编程实现 golang 常用的日期方法和时区的坑 golang SQLite3性能测试 golang的多协程实践 golang redis的模式订阅 java开发的zimg客户端 CAS单点登录实践(spring cas client配置) 81进制,用多进制方式把一个长长的整数变短 chrome浏览器自动填充失效问题 spring 登录提示 Bad credentials spring 项目tomcat 8.0.2 发布报错:Could not initialize class org.hibernate.validator.engine.ConfigurationImpl spring tiles界面为空白 activiti部署到linux后流程图不显示汉字的问题
golang 实现并发计算文件数量
程序员老刘 · 2015-11-21 · via 博客园 - 程序员老刘
package main

import (
	"fmt"
	"io/ioutil"
	"os"
)

func listDir(path string, ch chan int) {
	fmt.Println("waiting .....  read path:" + path)
	files, _ := ioutil.ReadDir(path)
	FileSlice := []string{}
	DirSlice := []string{}
	for _, fi := range files {
		if fi.IsDir() {
			//listDir(path + "/" + fi.Name())
			DirSlice = append(DirSlice, path+"/"+fi.Name())
			//fmt.Println("dir:" + path + "/" + fi.Name())
		} else {
			FileSlice = append(FileSlice, path+"/"+fi.Name())

		}
	}

	//return slice
	var count int
	count = 0
	//var FindFiles string
	for _, value := range FileSlice {
		fmt.Println("file:" + value)
		//FindFiles += value + ";"
		count++
	}

	//find sub directory by go pattern
	DirCount := len(DirSlice)
	fmt.Println("dir len:", DirCount)
	if DirCount > 0 {
		DirCH := make([]chan int, DirCount)
		i := 0
		for _, value := range DirSlice {
			fmt.Println("dir:" + value)
			DirCH[i] = make(chan int)
			go listDir(value, DirCH[i])
			i++
		}

		fmt.Println("wait routine return")
		for _, chs := range DirCH {
			returnCount := <-chs
			fmt.Println("return count=", returnCount)
			count += returnCount
			//			ReturnFiles := <-chs
			//			if len(ReturnFiles) > 0 {
			//				FindFiles += ReturnFiles + ";"
			//				fmt.Println("list dir chan<-" + ReturnFiles)
			//			}
		}

	}
	//return results
	fmt.Println("count=", count)
	ch <- count //FindFiles
}

func main() {
	if len(os.Args) < 2 {
		fmt.Println("please input param of path.")
		return
	}
	fmt.Println(os.Args[1])
	
	path := os.Args[1] //"C:/Users/Administrator/Desktop/publish/test"
	//path := "c:/QQSave"
	ch := make(chan int)
	go listDir(path, ch)
	fmt.Println("chan<-", <-ch)
}