Skip to content

Instantly share code, notes, and snippets.

@flyinprogrammer
Created February 15, 2016 00:37
Show Gist options
  • Save flyinprogrammer/c560e6f60a524539af6f to your computer and use it in GitHub Desktop.
Save flyinprogrammer/c560e6f60a524539af6f to your computer and use it in GitHub Desktop.
dup2 from The Go Programming Language (ISBN-13: 978-0134190440)
// dup2
//
// Prints the count and text of the lines that appear more than once in the input.
// It reads from stdin or from a list of named files.
//
// The original version simply printed the line and total count across all inputs.
// The exercise asked you to modify it to print all the anmes of the files in which
// each duplicated line occurs.
//
package main
import (
"bufio"
"fmt"
"os"
)
// LineInfo is a struct for keeping track
// of line information as we process files.
type LineInfo struct {
Files map[string]int
Count int
}
func main() {
counts := make(map[string]LineInfo)
files := os.Args[1:]
if len(files) == 0 {
countLines(os.Stdin, counts)
} else {
for _, arg := range files {
f, err := os.Open(arg)
if err != nil {
fmt.Fprintf(os.Stderr, "dup2: %v\n", err)
continue
}
countLines(f, counts)
f.Close()
}
}
for line, lineInfo := range counts {
fmt.Printf("Line: %s\n", line)
fmt.Printf("\tTotal Usage: %d\n", lineInfo.Count)
for fileName, count := range lineInfo.Files {
fmt.Printf("\t\t%s\t%d\n", fileName, count)
}
}
}
func countLines(f *os.File, counts map[string]LineInfo) {
input := bufio.NewScanner(f)
for input.Scan() {
lineInfo := counts[input.Text()]
lineInfo.Count++
if lineInfo.Files == nil {
lineInfo.Files = make(map[string]int)
}
lineInfo.Files[f.Name()]++
counts[input.Text()] = lineInfo
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment