dup1.go 733 B

12345678910111213141516171819202122232425262728
  1. // Dup1 prints the text of each line that appears more than
  2. // once in the standard input, preceded by its count.
  3. package main
  4. import (
  5. "bufio"
  6. "fmt"
  7. "os"
  8. )
  9. func DUP1() {
  10. fmt.Printf("----------dup1 start ---------------")
  11. counts := make(map[string]int) //映射,key是string、value是int
  12. input := bufio.NewScanner(os.Stdin) //读取 终端输入
  13. fmt.Printf("please input:\n")
  14. for input.Scan() && input.Text() != "end!" {
  15. counts[input.Text()]++
  16. // fmt.Printf("value:%s\n", input.Text())
  17. }
  18. // NOTE: ignoring potential errors from input.Err()
  19. fmt.Printf("\ninput end!\n")
  20. for line, n := range counts {
  21. // if n > 1 {
  22. fmt.Printf("%d\t%s\n", n, line)
  23. // }
  24. }
  25. fmt.Printf("----------dup1 end ---------------")
  26. }