diff options
author | Sameer Ajmani <sameer@golang.org> | 2014-03-12 23:43:55 -0400 |
---|---|---|
committer | Sameer Ajmani <sameer@golang.org> | 2014-03-12 23:43:55 -0400 |
commit | 9aa2e1143c55cfa6a0b8651067b34c61da8d5646 (patch) | |
tree | f0eb469d9b435c085c77a943225c9eef72d84eb3 /content/pipelines/serial.go | |
parent | 401e4f4a7cd251187c7e3b18b1dc2c583e6d2e45 (diff) |
go.blog: pipelines and cancellation.
R=adg, r, rsc, ken, bcmills
CC=adonovan, campoy, david.crawshaw, gmlewis, golang-codereviews
https://golang.org/cl/71070047
Diffstat (limited to 'content/pipelines/serial.go')
-rw-r--r-- | content/pipelines/serial.go | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/content/pipelines/serial.go b/content/pipelines/serial.go new file mode 100644 index 0000000..7a44ca1 --- /dev/null +++ b/content/pipelines/serial.go @@ -0,0 +1,53 @@ +package main + +import ( + "crypto/md5" + "fmt" + "io/ioutil" + "os" + "path/filepath" + "sort" +) + +// MD5All reads all the files in the file tree rooted at root and returns a map +// from file path to the MD5 sum of the file's contents. If the directory walk +// fails or any read operation fails, MD5All returns an error. +func MD5All(root string) (map[string][md5.Size]byte, error) { + m := make(map[string][md5.Size]byte) + err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error { // HL + if err != nil { + return err + } + if info.IsDir() { + return nil + } + data, err := ioutil.ReadFile(path) // HL + if err != nil { + return err + } + m[path] = md5.Sum(data) // HL + return nil + }) + if err != nil { + return nil, err + } + return m, nil +} + +func main() { + // Calculate the MD5 sum of all files under the specified directory, + // then print the results sorted by path name. + m, err := MD5All(os.Args[1]) // HL + if err != nil { + fmt.Println(err) + return + } + var paths []string + for path := range m { + paths = append(paths, path) + } + sort.Strings(paths) // HL + for _, path := range paths { + fmt.Printf("%x %s\n", m[path], path) + } +} |