I’m really new to Go and reading the book, “The Go Programming Language”.
One exercise had me modifying code to capture filenames along with the count of duplicate lines.
My problem with the code below is the following three lines:
s := fmt.Sprintf("%d\t", n)
s += fmt.Sprintf("%s\n", line)
s += fmt.Sprintln(files[line])
if I change the second line to
s += fmt.Sprintf("%s\t", line)
it only outputs the file names (the last line). I’ve tried single line versions for Printf, Sprintf and the multi-line versions with Print and Println, they all do the same thing. I’m using go version 1.9.2 on Windows. Searching didn’t seem to uncover limits with the Print functions. Any pointers on what I should look for next or what I’m doing wrong would be appreciated.
import (
"fmt"
"io/ioutil"
"os"
"strings"
)
func main() {
counts := make(map[string]int)
files := make(map[string]string)
for _, filename := range os.Args[1:] {
data, err := ioutil.ReadFile(filename)
if err != nil {
fmt.Fprintf(os.Stderr, "dup3: %v\n", err)
continue
}
for _, line := range strings.Split(string(data), "\n") {
counts[line]++
files[line] += " " + filename
}
}
for line, n := range counts {
if n > 1 {
s := fmt.Sprintf("%d\t", n)
s += fmt.Sprintf("%s\n", line)
s += fmt.Sprintln(files[line])
fmt.Print(s)
}
}
}