π Scenario: Imagine you are working with text data in Go. Strings in Go are different from some other languages because they are made of bytes and are immutable. Understanding how to work with strings and convert them to runes (characters) is important for correct text processing.
π― Goal: You will create a string, convert it to a slice of runes to access characters properly, and then print the characters one by one.
π What You'll Learn
Create a string variable with the exact value "Hello, δΈη"
Create a variable that converts the string to a slice of runes
Use a for loop with index and character variables named i and r to iterate over the rune slice
Print each character on a new line using fmt.Println
π‘ Why This Matters
π Real World
Text processing in Go often requires handling Unicode characters correctly, such as when reading user input, processing files, or working with network data.
πΌ Career
Understanding how strings and runes work in Go is essential for software developers working on internationalized applications, web servers, or any system that processes text data.
Progress0 / 4 steps
1
Create the string variable
Create a string variable called text and set it to the exact value "Hello, δΈη"
DSA Go
Hint
Use text := "Hello, δΈη" to create the string variable.
2
Convert string to rune slice
Create a variable called runes that converts the string text to a slice of runes using []rune(text)
DSA Go
Hint
Use runes := []rune(text) to convert the string to runes.
3
Iterate over the rune slice
Use a for loop with variables i and r to iterate over runes using for i, r := range runes
DSA Go
Hint
Use for i, r := range runes to loop over the rune slice.
4
Print each character
Inside the for loop, use fmt.Println(string(r)) to print each character on a new line
DSA Go
Hint
Use fmt.Println(string(r)) inside the loop to print each character.
Practice
(1/5)
1. In Go, what is the type of a string internally? var s = "hello"
easy
A. A sequence of bytes representing UTF-8 encoded characters
B. An array of Unicode code points (runes)
C. A linked list of characters
D. A pointer to a null-terminated character array
Solution
Step 1: Understand Go string representation
Go strings are immutable sequences of bytes, typically UTF-8 encoded.
Step 2: Compare with other languages
Unlike some languages that use arrays of Unicode code points, Go uses bytes internally.
Final Answer:
A sequence of bytes representing UTF-8 encoded characters -> Option A
Quick Check:
Go strings = UTF-8 bytes [OK]
Hint: Remember Go strings are byte sequences, not rune arrays [OK]
Common Mistakes:
Thinking Go strings store runes directly
Assuming null-terminated strings like C
Confusing with linked list or array types
2. Which of the following is the correct way to get the length of a string in Go? s := "hello"
easy
A. length(s)
B. s.length()
C. len(s)
D. s.len
Solution
Step 1: Recall Go built-in function for length
Go uses the built-in function len() to get the length of strings and slices.
Step 2: Check other options
Methods like s.length() or s.len do not exist in Go; length(s) is not valid syntax.
Final Answer:
len(s) -> Option C
Quick Check:
Use len() for string length in Go [OK]
Hint: Use len() function to get string length in Go [OK]
Common Mistakes:
Using method calls like s.length() from other languages
Trying to access length as a property
Using undefined functions like length()
3. What will be the output of this Go code?
package main
import "fmt"
func main() {
s := "Goπ"
fmt.Println(len(s))
}
medium
A. 3
B. 6
C. 4
D. 5
Solution
Step 1: Analyze string content and encoding
The string "Goπ" has 'G' and 'o' as 1 byte each, and the emoji 'π' is a 4-byte UTF-8 character.
Step 2: Calculate total byte length
Total bytes = 1 (G) + 1 (o) + 4 (π) = 6 bytes.
Step 3: Check len() output
len(s) returns the number of bytes, so output is 6.
Final Answer:
6 -> Option B
Quick Check:
len() counts bytes, not characters [OK]
Hint: len() returns bytes count, not character count [OK]
Common Mistakes:
Assuming len() returns number of characters
Ignoring multi-byte UTF-8 characters
Confusing bytes with runes
4. Identify the error in this Go code that tries to print each character of a string:
package main
import "fmt"
func main() {
s := "Go"
for i := 0; i < len(s); i++ {
fmt.Println(s[i])
}
}
medium
A. It prints byte values, not characters
B. Syntax error in for loop
C. s[i] is invalid syntax
D. Missing import for fmt
Solution
Step 1: Understand s[i] in Go strings
Accessing s[i] returns the byte at index i, not a character or string.
Step 2: Check output of fmt.Println(s[i])
It prints the numeric byte value (ASCII code), not the character itself.
Final Answer:
It prints byte values, not characters -> Option A
Quick Check:
s[i] = byte, prints number [OK]
Hint: s[i] gives byte, convert to rune or string to print char [OK]
Common Mistakes:
Expecting s[i] to print characters directly
Ignoring byte vs rune difference
Assuming syntax error where none exists
5. How can you correctly iterate over each Unicode character in a Go string to print them? Choose the correct code snippet.
hard
A. for i := 0; i < len(s); i++ { fmt.Println(string(s[i])) }
B. for i := range s { fmt.Println(s[i]) }
C. for i, c := range s { fmt.Println(s[i]) }
D. for _, r := range s { fmt.Println(string(r)) }
Solution
Step 1: Understand iteration over strings in Go
Using 'for _, r := range s' iterates over runes (Unicode code points), not bytes.
Step 2: Check printing each character
Converting rune 'r' to string prints the actual character correctly.
Step 3: Analyze other options
for i := 0; i < len(s); i++ { fmt.Println(string(s[i])) } prints bytes as strings, which may break multi-byte chars; C and D print bytes, not runes.
Final Answer:
for _, r := range s { fmt.Println(string(r)) } -> Option D
Quick Check:
Use range to get runes for correct Unicode iteration [OK]
Hint: Use 'for _, r := range s' to iterate Unicode chars [OK]
Common Mistakes:
Iterating by byte index instead of runes
Printing bytes as characters causing errors
Confusing index and rune variables
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.