mirror of
https://github.com/gopl-zh/gopl-zh.github.com.git
synced 2025-08-19 12:41:33 +00:00
good good study, day day up!
This commit is contained in:
48
vendor/gopl.io/ch1/dup2/main.go
generated
vendored
Normal file
48
vendor/gopl.io/ch1/dup2/main.go
generated
vendored
Normal file
@@ -0,0 +1,48 @@
|
||||
// Copyright © 2016 Alan A. A. Donovan & Brian W. Kernighan.
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
|
||||
// See page 10.
|
||||
//!+
|
||||
|
||||
// Dup2 prints the count and text of lines that appear more than once
|
||||
// in the input. It reads from stdin or from a list of named files.
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
counts := make(map[string]int)
|
||||
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, n := range counts {
|
||||
if n > 1 {
|
||||
fmt.Printf("%d\t%s\n", n, line)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func countLines(f *os.File, counts map[string]int) {
|
||||
input := bufio.NewScanner(f)
|
||||
for input.Scan() {
|
||||
counts[input.Text()]++
|
||||
}
|
||||
// NOTE: ignoring potential errors from input.Err()
|
||||
}
|
||||
|
||||
//!-
|
Reference in New Issue
Block a user