range_loop.go 373 B

1234567891011121314151617181920
  1. package main
  2. import "fmt"
  3. func countChars(s string) map[rune]int {
  4. counts := make(map[rune]int)
  5. for _, c := range s {
  6. counts[c]++
  7. }
  8. return counts
  9. }
  10. func main() {
  11. str := "Hello, 世界! 👋"
  12. fmt.Printf("%q\n", countChars(str))
  13. fmt.Println("--------------------------------")
  14. for k, v := range countChars(str) {
  15. fmt.Printf("%q 出现了 %d 次\n", k, v)
  16. }
  17. }