Sobes.tech
Junior — Middle

What value will be returned by the len function when called on a string?

sobes.tech AI

Answer from AI

In Go, the len function when called on a string returns the number of bytes in that string, not the number of characters (runes).

This is because strings in Go are sequences of bytes in UTF-8 encoding. If the string contains only ASCII characters, the number of bytes matches the number of characters. But if there are multibyte characters (for example, Cyrillic, hieroglyphs), then len will return the total number of bytes, not characters.

Example:

s := "Привет"
fmt.Println(len(s)) // Will output the number of bytes, e.g., 12

runes := []rune(s)
fmt.Println(len(runes)) // Will output the number of runes (characters), e.g., 6

Thus, len for a string is the length in bytes.

What value will be returned by the len function when… - sobes.tech