[Go] Count Unicode code points in a UTF-8 string

Created on November 30, 2026

utf8.RuneCountInString counts Unicode code points, which Go calls runes. The Japanese example below contains eight runes. len would count the UTF-8 bytes.

package main

import (
    "fmt"
    "unicode/utf8"
)

func main() {
    str := "これはテストです"
    fmt.Println(utf8.RuneCountInString(str))
}
8

A character that appears as one unit on screen can be made of more than one code point.

Go