Go 编程:集合类型与程序基础
1. 集合类型相关函数
在 Go 编程中,集合类型的处理是很重要的一部分。下面为大家介绍一些相关的函数。
1.1 按频率报告函数
有一个 reportByFrequency 函数,它的作用是根据单词出现的频率进行报告。以下是该函数的代码:
func reportByFrequency(wordsForFrequency map[int][]string) {
frequencies := make([]int, 0, len(wordsForFrequency))
for frequency := range wordsForFrequency {
frequencies = append(frequencies, frequency)
}
sort.Ints(frequencies)
width := len(fmt.Sprint(frequencies[len(frequencies)-1]))
fmt.Println("Frequency →Words")
for _, frequency := range frequencies {
words := wordsForFrequency[frequency]
sort.Strings(words)
fmt.Printf("%*d %s\n", width, frequency, strings.Join(words, ", "))
}
}
超级会员免费看
订阅专栏 解锁全文

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



