Go语言实用功能与包的全面解析
1. 单词计数示例
以下是一个简单的Go语言代码,用于统计文件中单词的数量:
path := `...\words.txt` // point to a real file
counts, err := CountWordsInFile(path)
if err != nil {
fmt.Printf("Count failed: %v\n", err)
return
}
fmt.Printf("Counts for %q:\n", path)
for k, v := range counts {
fmt.Printf(" %-20s = %v\n", k, v)
}
假设文件内容为: Now is the time to come to the aid of our countrymen! ,运行该代码后输出如下:
Counts for ".../words.txt":
Now = 1
time = 1
come = 1
countrymen! = 1
our = 1
is = 1
the = 2
to = 2
aid
超级会员免费看
订阅专栏 解锁全文
332

被折叠的 条评论
为什么被折叠?



