Character Counter

Count every character in a string using a map[rune]int. A rune is Go's type for a single character. When you loop over a string with range, each value is a rune.

package main

import "fmt"

func main() {
	text := "hello, world!"

	counts := map[rune]int{}
	for _, ch := range text {
		counts[ch]++
	}

	for ch, count := range counts {
		fmt.Printf("'%c': %d\n", ch, count)
	}
}
▶ Open Go Playground

Copy the code above and paste to run

© 2026 ByteLearn.dev. Free courses for developers. · Privacy