源文件 博客/管道/serial.go
1
2
3 package main
4
5 import (
6 "crypto/md5"
7 "fmt"
8 "io/ioutil"
9 "os"
10 "path/filepath"
11 "sort"
12 )
13
14
15
16
17 func MD5All(root string) (map[string][md5.Size]byte, error) {
18 m := make(map[string][md5.Size]byte)
19 err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
20 if err != nil {
21 return err
22 }
23 if !info.Mode().IsRegular() {
24 return nil
25 }
26 data, err := ioutil.ReadFile(path)
27 if err != nil {
28 return err
29 }
30 m[path] = md5.Sum(data)
31 return nil
32 })
33 if err != nil {
34 return nil, err
35 }
36 return m, nil
37 }
38
39 func main() {
40
41
42 m, err := MD5All(os.Args[1])
43 if err != nil {
44 fmt.Println(err)
45 return
46 }
47 var paths []string
48 for path := range m {
49 paths = append(paths, path)
50 }
51 sort.Strings(paths)
52 for _, path := range paths {
53 fmt.Printf("%x %s\n", m[path], path)
54 }
55 }
56
查看纯文本