In Go, the length of a string can be calculated using the built-in len()
function.
Here's an example:
main.go185 chars10 lines
Note that in Go, strings are encoded in UTF-8 encoding, so the len()
function returns the number of bytes in the string, not the number of characters. To get the number of characters, you can use the utf8.RuneCountInString()
function:
main.go227 chars13 lines
This will correctly give the length of the string as 9, which is the number of Unicode code points in the string (including the Chinese characters).
gistlibby LogSnag