Count Rune Frequency Across Text
Tally each Unicode code point with a map keyed by rune, then report counts in sorted order so the histogram is deterministic.
Code
package main
import (
"fmt"
"sort"
)
func main() {
text := "gögogo"
counts := make(map[rune]int)
for _, r := range text {
counts[r]++
}
runes := make([]rune, 0, len(counts))
for r := range counts {
runes = append(runes, r)
}
sort.Slice(runes, func(i, j int) bool { return runes[i] < runes[j] })
fmt.Println("byte length:", len(text))
fmt.Println("rune count:", len([]rune(text)))
for _, r := range runes {
fmt.Printf("%q x%d\n", r, counts[r])
}
}What the code performs
The moving parts are the function main.
for _, r := range text is the loop that carries the per-item work.
Points of care
- The platform already ships each API used, so no installation precedes the run.
Applying it
The entry-point signature is func main()
The run prints byte length:, rune count: — each prefix names the step that produced it.
Statements that matter
sort.Slice(runes, func(i, j int) bool { return runes[i] < runes[j] })
for _, r := range runes {
fmt.Printf("%q x%d\n", r, counts[r])
}
for _, r := range text {
counts[r]++
}
Interactive Tools for This Topic
Character Frequency AnalyzerCount how often each character appears in a text, with Unicode code points, optional case folding, and whitespace exclusion.cURL to Python & Go ConverterTranslate curl CLI commands with headers, auth, JSON bodies, and timeouts into ready-to-run Python requests and Go net/http client code snippets.SRI Hash GeneratorGenerate Subresource Integrity (SRI) hashes from resource content using SHA-256, SHA-384, or SHA-512, and copy a ready-to-use script tag.Word Frequency CounterCount word occurrences in text with Unicode-aware tokenization, optional lowercasing, and a ranked frequency table.
FAQ
- What does Count Rune Frequency Across Text print when run?
- No setup needed: run the file and look for labeled lines like `byte length:`, `rune count:` in the output.
Report a problem with this exampleOpens a pre-filled issue on GitHub.