Go – Strings Overview
Estimated reading: 3 minutes 326 views

Go String Length – Measure String Size with len() in Go (2025 Guide)

Introduction – How to Find String Length in Go?

In Go, the length of a string is measured using the built-in len() function. However, it’s important to know that len() returns the number of bytes, not characters. This matters especially when working with Unicode or multibyte characters like emojis or accented letters.

In this section, you’ll learn:

  • How to use len() to get string length
  • Difference between byte length and character count
  • How to count Unicode characters (runes)
  • Best practices when handling multi-language strings

Basic Syntax – Get String Length with len()

s := "Hello"
fmt.Println(len(s))

Output:

5

len(s) returns the number of bytes in the string.


Example – Multibyte Characters (Unicode)

s := "Go"
fmt.Println(len(s))

Output:

6

Although it looks like 3 characters, len(s) returns 6 bytes because the emoji takes 4 bytes.


Count Unicode Characters (Runes) Using utf8.RuneCountInString()

To get the actual number of characters (runes), use the utf8 package:

import "unicode/utf8"

s := "Go"
fmt.Println(utf8.RuneCountInString(s))

Output:

3

This returns the correct character count, not just byte count.


What’s a Rune?

In Go:

  • A string is a sequence of bytes
  • A rune is a Unicode code point, usually a character

String Length Comparison Table

Stringlen() (Bytes)utf8.RuneCountInString() (Characters)
"Hello"55
"Go"63
"こんにちは"155

Use the right function based on your use case!


Summary – Recap & Next Steps

In Go, measuring string length depends on whether you care about bytes or characters. Use len() for low-level byte processing and utf8.RuneCountInString() for human-readable character count.

Key Takeaways:

  • len(string) returns byte length
  • Unicode characters may span multiple bytes
  • Use utf8.RuneCountInString() to get actual character count
  • Ideal for UI, validation, and multilingual support

Next: Learn about Go String Manipulation like concatenation, substring, and comparison techniques.


FAQs – Go String Length

What does len(s) return for a string in Go?
It returns the number of bytes, not characters.

How do I count characters in a Unicode string in Go?
Use utf8.RuneCountInString(s) from the unicode/utf8 package.

Why is len("") equal to 4?
Because the emoji is stored using 4 bytes in UTF-8 encoding.

Can I use len() for string validation?
Only if you care about byte limits. For character count (e.g. input length), use utf8.RuneCountInString().

Is len([]rune(s)) same as utf8.RuneCountInString(s)?
Yes. Converting the string to a rune slice and taking its length gives the same result.


Share Now :
Share

Go String Length

Or Copy Link

CONTENTS
Scroll to Top