From d047ea9eb62367e0d03a5eba1411c1485e01bf34 Mon Sep 17 00:00:00 2001 From: Igor V Belousov Date: Thu, 18 Nov 2021 20:46:20 +0300 Subject: [PATCH] Chapter 1 - dup3 --- ch1/dup3.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 ch1/dup3.go diff --git a/ch1/dup3.go b/ch1/dup3.go new file mode 100644 index 0000000..935a9b5 --- /dev/null +++ b/ch1/dup3.go @@ -0,0 +1,27 @@ +package main + +import ( + "fmt" + "io/ioutil" + "os" + "strings" +) + +func main() { + counts := make(map[string]int) + for _, filename := range os.Args[1:] { + data, err := ioutil.ReadFile(filename) + if err != nil { + fmt.Fprintf(os.Stderr, "dup3: %v\v", err) + continue + } + for _, line := range strings.Split(string(data), "\n") { + counts[line]++ + } + } + for line, n := range counts { + if n > 1 { + fmt.Printf("%d\t%s\n", n, line) + } + } +} \ No newline at end of file