CHAPTER 12
Beginner
Strings and Runes
Updated: May 17, 2026
5 min read
# CHAPTER 12
Strings and Runes
1. Introduction
Handling text seems simple:text := "Hello". But what happens when your text contains emojis (😀), Japanese kanji (語), or Arabic script? Modern backend systems must handle global data flawlessly. Go was created by the same engineers who invented the UTF-8 encoding standard, making Go's approach to strings uniquely powerful, but slightly different from older languages.
2. Learning Objectives
By the end of this chapter, you will be able to:- Understand that a String is an immutable slice of Bytes.
- Understand the difference between a Byte and a Rune.
-
Use the
stringspackage for manipulation.
- Iterate over strings safely without breaking multi-byte characters.
3. Strings are Immutable Byte Slices
In Go, astring is simply a read-only (immutable) slice of bytes (uint8).
Once a string is created, its contents cannot be changed.
go
4. The Problem with Bytes (UTF-8)
In the old ASCII system, every character was exactly 1 byte. 'A' is 1 byte. In modern UTF-8, an English letter is 1 byte, but an emoji or Chinese character might take 3 or 4 bytes!If you use len() on a string, it returns the number of bytes, NOT the number of human-readable characters!
go
5. Enter the rune
To solve this, Go introduced the rune data type. A rune is an alias for int32. It holds the exact Unicode value of a character, regardless of whether it takes 1 byte or 4 bytes.
If you want to count the actual human-readable characters, you convert the string to a slice of runes, or use the utf8 package.
go
6. Single Quotes vs Double Quotes
-
Double Quotes (
" "): Used for Strings."A"is a string.
-
Single Quotes (
' '): Used for Runes.'A'is a rune (it actually represents the number 65, its Unicode value).
7. The strings Package
Go provides a robust standard library for string manipulation.
*(Requires import "strings")*
go
8. Common Mistakes
-
Indexing a multi-byte string: If you do
char := emoji[6], you are grabbing the first byte of a 3-byte Chinese character. It will print random garbage. Always convert the string to a[]runeif you need to access specific characters by index.
-
String Concatenation in Loops: Strings are immutable.
text += "a"creates a brand new string in memory every time. Doing this 10,000 times causes massive memory lag. Usestrings.Builderfor heavy string generation.
9. Best Practices
-
Use
for index, char := range myStringto iterate over strings. Therangekeyword is smart enough to automatically decode UTF-8 bytes into whole Runes!
10. Exercises
-
1.
Write a program that defines
word := "Golang".
-
2.
Use the
stringspackage to print whether it contains the substring "go" (Hint: Make sure to handle case-sensitivity!).
11. MCQs with Answers & Explanations
Question 1
In Go, what is a string fundamentally made of?
text[0] = 'A')?
a) Yes b) No, strings are immutable
Answer: b) No, strings are immutable.
Question 3
What does len("Hello 😀") return?
Question 4
What is a rune in Go?
Question 5
How do you properly find the number of human-readable characters in a string containing emojis?
Question 6
What does "A" (double quotes) represent?
Question 7
What does 'A' (single quotes) represent?
Question 8
Which package provides functions like ToUpper, Contains, and ReplaceAll?
Question 9
If you loop over a string using for ... range, what does the loop automatically yield?
Question 10
For high-performance string building inside a large loop, what should you use?
12. Interview Preparation
Interview Questions:-
1.
Explain the difference between
len("Golang")andlen("世界").
- 2. Differentiate between a byte, a rune, and a string in Go.
13. Summary
Go's text handling is deeply tied to the UTF-8 standard. By understanding that strings are just immutable slices of bytes, and that characters (runes) can be of variable width, you avoid the encoding bugs that plague older languages. Thestrings and utf8 packages provide all the tools needed for robust text manipulation.