Hacker News new | ask | show | jobs
by weberc2 3580 days ago
To clarify, Letting the length of a UTF-8 string in Go is O(1); it's computed and stored on the string header at creation.
1 comments

To clarify even more: that length is the number of bytes (or UTF-8 code units) in the string. It doesn't corresponding to the number of characters (which one may either consider to be Unicode codepoints, or more technically correct, Unicode grapheme clusters).

If you want to count the number of codepoints in a string (called "rune" in Go), then you need to do so explicitly: https://golang.org/pkg/unicode/utf8/#RuneCountInString

Touché