| 1234567891011121314151617181920 |
- package main
- import "fmt"
- func countChars(s string) map[rune]int {
- counts := make(map[rune]int)
- for _, c := range s {
- counts[c]++
- }
- return counts
- }
- func main() {
- str := "Hello, 世界! 👋"
- fmt.Printf("%q\n", countChars(str))
- fmt.Println("--------------------------------")
- for k, v := range countChars(str) {
- fmt.Printf("%q 出现了 %d 次\n", k, v)
- }
- }
|