package main标签:map,某字段,string,fre,int,fmt,frequency,GO From: https://www.cnblogs.com/lisus2000/p/17054069.html
import (
"fmt"
"sort"
)
type frequency struct {
char string
fre int
}
func frequencies(s string) []frequency {
m := make(map[string]int)
for _, r := range s {
m[string(r)]++
}
a := make([]frequency, 0, len(m))
for c, f := range m {
a = append(a, frequency{char: c, fre: f})
}
sort.Slice(a, func(i, j int) bool {
return a[i].fre > a[j].fre
})
return a
}
func main() {
str := "hiilovegogo"
fmt.Printf(str)
f := frequencies(str)
fmt.Println(f)
}