《go语言圣经》练习答案--1.3. 查找重复的行

练习 1.4: 修改dup2,出现重复的行时打印文件名称。

原始 dup2
// 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()
}
修改后 dup2
package main

import (
	"bufio"
	"fmt"
	"os"
)

type nameAndCount struct {
	filename []string
	count    int
}

func main() {
	counts := make(map[string]*nameAndCount)
	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 k, v := range counts {
		if v.count > 1 {
			fmt.Printf("\n%d %s %v\n", v.count, k, v.filename)
		}
	}
}

func countLines(f *os.File, counts map[string]*nameAndCount) {
	input := bufio.NewScanner(f)
	for input.Scan() {
		key := input.Text()
		if _, ok := counts[key]; ok {
			counts[key].count++
			counts[key].filename = append(counts[key].filename, f.Name())
		} else {
			counts[key] = &nameAndCount{
				make([]string, 1),
				1,
			}
			counts[key].filename[0] = f.Name()
		}
	}
}
输出测试:
PS C:\ForProgram\Codes\Go\hellogo> go run test.go
123
123
456
2 123 [/dev/stdin /dev/stdin]
PS C:\ForProgram\Codes\Go\hellogo> go run test.go 1.txt 2.txt

4 789 [1.txt 1.txt 2.txt 2.txt]

2 000 [1.txt 2.txt]

4 123 [1.txt 1.txt 2.txt 2.txt]

4 456 [1.txt 1.txt 2.txt 2.txt]

 

posted @ 2022-03-08 14:24  随风而逝的白色相簿  阅读(166)  评论(0编辑  收藏  举报