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 = 1
of = 1
<
Go标准库功能解析与实践
超级会员免费看
订阅专栏 解锁全文

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



