```
// Dup1 prints the text of each line that appears more than
// once in the standard input, preceded by its count.
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
counts := make(map[string]int)
input := bufio.NewScanner(os.Stdin)
for input.Scan() {
counts[input.Text()]++
}
// NOTE: ignoring potential errors from input.Err()
// 循环map, py counts,出错了很多次,到了go,合法了
for line, n := range counts {
if n > 1 {
fmt.Printf("%d\t%s\n", n, line)
}
}
}
```
** input.Scan() ** 在命令行输入什么,它才会觉得我不输入了?
func main() {
counts := make(map[string]int)
input := bufio.NewScanner(os.Stdin)
for input.Scan() {
if input.Text()=="e"{break}//设置退出for循环
counts[input.Text()]++//输入相同的value会导致对应的key值增加
fmt.Println(counts)
}
// NOTE: ignoring potential errors from input.Err()
for line, n := range counts {
if n > 1 {
fmt.Printf("%d\t%s\n", n, line)
}
}
}
//手动设置退出 ```
#1
更多评论
func main() {
counts := make(map[string]int)
input := bufio.NewScanner(os.Stdin)
for input.Scan() {
if input.Text()=="e"{break}
counts[input.Text()]++//输入相同的value会导致对应的key值增加
fmt.Println(counts)
}
// NOTE: ignoring potential errors from input.Err()
for line, n := range counts {
if n > 1 {
fmt.Printf("%d\t%s\n", n, line)
}
}
}
#2