go学习笔记:计算文件中重复的行,并输出重复次数

你。 提交于 2020-10-01 13:43:49

方法一:按行读取
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()]++
}
}
方法二:一次性读入到内存中,再按行处理
package main






import (
"fmt"
"io/ioutil"
"os"
"strings"
)




func main() {
counts:=make(map[string]int)
for ,filename:=range os.Args[1:]{
data,err:=ioutil.ReadFile(filename)
if err!=nil{
fmt.Fprintf(os.Stderr,"dup2:%v\n",err)
continue
}
for





,line:=range strings.Split(string(data),"\n"){
counts[line]++
}
}
for line,n:=range counts{
if n >1{
fmt.Printf("%d\t%s\n",n,line)
}
}
}










标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!