Sobes.tech
Junior — Middle

What value does the len function return when applied to a string data type?

sobes.tech AI

Answer from AI

In the Go language, the len function when applied to a string returns the number of bytes in that string, not the number of characters (runes). This is because strings in Go are stored in UTF-8 format, where one character can occupy multiple bytes.

Example:

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

If you need to get the number of characters (runes), you should convert the string to a slice of runes:

fmt.Println(len([]rune(s))) // Will output the number of characters, for example 6