Go – String Length
To find the length of a string in Go, first decide what you need to count. The built-in len() function returns the number of bytes in a string, while len([]rune(str)) returns the number of Unicode code points.
For strings containing only ASCII characters, the byte count and rune count are the same. For strings containing characters encoded with multiple UTF-8 bytes, such as £, 界, or many emoji, the results can differ.
Go String Length in Bytes and Runes
len(str)returns the size of the string in bytes.len([]rune(str))returns the number of Unicode code points.utf8.RuneCountInString(str)also counts Unicode code points without explicitly creating a rune slice.
A Go string stores a read-only sequence of bytes, usually containing UTF-8 encoded text. Therefore, len(str) measures the stored byte sequence rather than the number of visible characters.
Syntax to Count Runes in a Go String
The syntax to find the length of string str is
len([]rune(str))
The expression returns an integer representing the number of Unicode code points in the string str.
You can also use the unicode/utf8 package when you only need the rune count.
utf8.RuneCountInString(str)
Find the Length of an ASCII String in Go
In the following program, we will define a string x and find its length using len() function.
example.go
package main
func main() {
var str = "Hello World"
var length = len([]rune(str))
println("Length of the string is :", length)
}
Output
Length of the string is : 11
Every character in Hello World is represented by one byte, including the space. Therefore, both len(str) and len([]rune(str)) return 11.
Compare Go String Size in Bytes with Rune Count
Now, let us take a string with some characters whose length is more than a byte, and find out the length of string.
example.go
package main
func main() {
var str = "ab£"
var length = len([]rune(str))
println("Length of the string is : ", length)
println("Output of len(str) is : ", len(str))
}
Output
Length of the string is : 3
Output of len(str) is : 4
Please observe the difference between len(string) and len([]rune(string)).
The letters a and b require one byte each, while £ requires two bytes in UTF-8. The string therefore contains three runes but four bytes.
Count Unicode Characters with utf8.RuneCountInString
The unicode/utf8 package provides RuneCountInString(), which counts the runes in a string directly.
package main
import (
"fmt"
"unicode/utf8"
)
func main() {
str := "Go界"
fmt.Println("Bytes:", len(str))
fmt.Println("Runes:", utf8.RuneCountInString(str))
}
Bytes: 5
Runes: 3
The characters G and o use one byte each. The character 界 uses three bytes in UTF-8, producing a total of five bytes and three runes.
Find the Length of an Empty Go String
An empty string contains no bytes and no runes. Both byte-length and rune-count expressions return zero.
package main
import "fmt"
func main() {
str := ""
fmt.Println(len(str))
fmt.Println(len([]rune(str)))
}
0
0
You can therefore test whether a string is empty with len(str) == 0 or with the simpler comparison str == "".
Why Rune Count May Differ from Visible Character Count
A rune represents a Unicode code point, but one user-perceived character can contain multiple code points. For example, an accented character may be stored as a base letter followed by a combining mark, and some emoji are formed from several code points.
package main
import (
"fmt"
"unicode/utf8"
)
func main() {
str := "e\u0301"
fmt.Println("Bytes:", len(str))
fmt.Println("Runes:", utf8.RuneCountInString(str))
fmt.Println("Text:", str)
}
Bytes: 3
Runes: 2
Text: é
The displayed text may look like one character, but it consists of two runes: e and a combining acute accent. Therefore, rune count should not always be interpreted as the number of visible characters or grapheme clusters.
Choose the Correct Go String Length Method
| Requirement | Go expression |
|---|---|
| Number of bytes | len(str) |
| Number of Unicode code points | len([]rune(str)) |
| Rune count without creating a rune slice | utf8.RuneCountInString(str) |
| Check whether a string is empty | str == "" or len(str) == 0 |
Use len(str) when working with storage size, protocol limits, byte offsets, or ASCII-only data. Use a rune count when your logic operates on Unicode code points.
Common Go String Length Mistakes
- Assuming
len(str)counts characters: it counts bytes. - Assuming every rune uses one byte: UTF-8 runes can use multiple bytes.
- Assuming rune count always equals visible character count: combining marks and joined emoji may contain several runes.
- Indexing a string by rune position:
str[i]accesses a byte, not necessarily a complete Unicode character. - Converting to
[]runeunnecessarily: useutf8.RuneCountInString()when only the count is required.
Go String Length Frequently Asked Questions
How do I find the length of a string in Go?
Use len(str) to get the number of bytes. Use len([]rune(str)) or utf8.RuneCountInString(str) to get the number of Unicode code points.
What does the len function return for a Go string?
For a string, len() returns the number of bytes in the string. It does not necessarily return the number of Unicode characters.
Why is len(str) larger than len([]rune(str))?
The string contains one or more Unicode characters encoded with multiple UTF-8 bytes. len(str) counts those bytes, while len([]rune(str)) counts code points.
Is len([]rune(str)) the number of visible characters?
Not always. A visible character can be formed from multiple Unicode code points, such as a letter combined with an accent or a multi-part emoji sequence.
How do I check whether a Go string is empty?
Use str == "" or len(str) == 0. Both correctly identify an empty string.
Go String Length Editorial QA Checklist
- Verify that
len(str)is described as a byte count, not a character count. - Confirm that rune examples use valid UTF-8 text and produce the stated byte and rune totals.
- Check that
utf8.RuneCountInString()examples importunicode/utf8. - Do not describe runes as always equivalent to visible characters.
- Confirm that string indexing examples, when present, explain that indexes refer to bytes.
- Verify that all output blocks exactly match their Go programs.
Go String Length Summary
Use len(str) to measure the size of a Go string in bytes. Use len([]rune(str)) or utf8.RuneCountInString(str) to count Unicode code points. For plain ASCII text, these values are equal, but they can differ for UTF-8 text.
In this Go Tutorial, we learned how to find the length of a string using runes and len() function.
TutorialKart.com