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

推荐订阅源

G
Google Developers Blog
WordPress大学
WordPress大学
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
小众软件
小众软件
人人都是产品经理
人人都是产品经理
美团技术团队
Blog — PlanetScale
Blog — PlanetScale
S
SegmentFault 最新的问题
博客园 - 【当耐特】
V
V2EX
Microsoft Azure Blog
Microsoft Azure Blog
博客园 - 叶小钗
Google DeepMind News
Google DeepMind News
量子位
罗磊的独立博客
月光博客
月光博客
N
Netflix TechBlog - Medium
大猫的无限游戏
大猫的无限游戏
博客园_首页
P
Proofpoint News Feed
Jina AI
Jina AI
云风的 BLOG
云风的 BLOG
博客园 - 司徒正美
腾讯CDC

博客园 - 程序员老刘

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)
}