Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexniver/32c0f609f631e708652ba58913de9b7c to your computer and use it in GitHub Desktop.
Save alexniver/32c0f609f631e708652ba58913de9b7c to your computer and use it in GitHub Desktop.
package main

import (
	"crypto/md5"
	"fmt"
	"io/ioutil"
	"os"
	"path/filepath"
	"runtime"
	//"sort"
	"time"
)

type Result struct {
	Path    string
	Md5byte [md5.Size]byte
	Err     error
}

func main() {
	runtime.GOMAXPROCS(runtime.NumCPU())
	startTime := time.Now()
	resultArr := MD5All("/home/zl/.gvm/")
	endTime := time.Now()

	fmt.Println(endTime.Sub(startTime))
	fmt.Println(len(resultArr))
}

func MD5All(root string) []*Result {
	resultArr := make([]*Result, 10)

	filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
		if err != nil {
			return err
		}

		if !info.Mode().IsRegular() {
			return nil
		}

		data, e := ioutil.ReadFile(path)
		if e != nil {
			return e
		}

		result := &Result{Path: path, Md5byte: md5.Sum(data)}

		resultArr = append(resultArr, result)

		return nil
	})

	return resultArr

}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment