π Scenario: Imagine you are working on a simple text editor. You need to understand how strings are stored and handled in memory to manage text efficiently.
π― Goal: Learn how to create strings, measure their length, and access individual characters in Go. Understand how strings are represented in memory by working with their byte values.
π What You'll Learn
Create a string variable with a specific value
Create a variable to store the length of the string
Use a loop to access each character of the string and print its byte value
Print the original string and its length
π‘ Why This Matters
π Real World
Understanding string memory helps in text processing, encoding, and optimizing storage in applications like text editors and communication tools.
πΌ Career
Many programming jobs require handling strings efficiently, especially when working with low-level data or performance-critical applications.
Progress0 / 4 steps
1
Create a string variable
Create a string variable called text and set it to the exact value "Hello, Go!".
DSA Go
Hint
Use text := "Hello, Go!" to create the string variable.
2
Get the length of the string
Create an integer variable called length and set it to the length of the string text using the built-in len function.
DSA Go
Hint
Use length := len(text) to get the string length.
3
Loop through the string bytes
Use a for loop with the variable i from 0 to length - 1 to access each byte of text. Inside the loop, print the byte value of text[i] using fmt.Printf with the format "%d ".
DSA Go
Hint
Use for i := 0; i < length; i++ and inside print text[i] as a number.
4
Print the string and its length
Print the string text using fmt.Println. Then print the length variable length on a new line using fmt.Println.
DSA Go
Hint
Use fmt.Println(text) and fmt.Println(length) after the loop.
Practice
(1/5)
1. What does a string represent in Go language? var s string = "hello"
easy
A. A sequence of bytes representing characters
B. A mutable array of characters
C. A pointer to an integer
D. A floating-point number
Solution
Step 1: Understand Go string type
In Go, a string is a sequence of bytes that represent characters, usually UTF-8 encoded.
Step 2: Check mutability and type
Strings in Go are immutable, so they cannot be changed after creation. They are not arrays or pointers to integers.
Final Answer:
A sequence of bytes representing characters -> Option A
Quick Check:
Go strings = immutable byte sequences [OK]
Hint: Remember: Go strings are immutable byte sequences [OK]
Common Mistakes:
Thinking strings are mutable arrays
Confusing strings with pointers
Assuming strings store integers
2. Which of the following is the correct way to declare and initialize a string in Go?
easy
A. var s string = 'hello'
B. var s string = "hello"
C. var s = hello
D. string s = "hello"
Solution
Step 1: Check string literal syntax in Go
Go uses double quotes "" for string literals, single quotes '' are for rune (character) literals.
Step 2: Validate declaration syntax
Correct declaration uses var keyword, type string, and double quotes for value.
Final Answer:
var s string = "hello" -> Option B
Quick Check:
Use double quotes for strings in Go [OK]
Hint: Use double quotes "" for strings, single quotes '' for runes [OK]
Common Mistakes:
Using single quotes for strings
Omitting quotes around string value
Using incorrect variable declaration syntax
3. What will be the output of the following Go code?
package main
import "fmt"
func main() {
s := "GoLang"
fmt.Println(len(s))
}
medium
A. 6
B. 7
C. 5
D. Error
Solution
Step 1: Understand the string value
The string "GoLang" has 6 characters: G, o, L, a, n, g.
Step 2: Use len function on string
len(s) returns the number of bytes in the string. Since all characters are ASCII, length is 6.
Final Answer:
6 -> Option A
Quick Check:
len("GoLang") = 6 [OK]
Hint: len() returns byte count for ASCII strings [OK]
Common Mistakes:
Counting characters incorrectly
Expecting len() to count runes instead of bytes
Assuming error due to missing imports
4. Identify the error in the following Go code snippet:
package main
import "fmt"
func main() {
s := "hello"
s[0] = 'H'
fmt.Println(s)
}
medium
A. No error; code runs fine
B. Missing import for fmt package
C. Incorrect string declaration syntax
D. Strings are immutable; cannot assign to s[0]
Solution
Step 1: Check string mutability in Go
Strings in Go are immutable, so you cannot change individual bytes like s[0].
Step 2: Analyze the assignment statement
Attempting s[0] = 'H' causes a compile-time error because strings cannot be modified.
Final Answer:
Strings are immutable; cannot assign to s[0] -> Option D
Quick Check:
Strings immutable = no s[0] assignment [OK]
Hint: Remember: Go strings cannot be changed after creation [OK]
Common Mistakes:
Trying to modify string bytes directly
Ignoring immutability errors
Confusing strings with byte slices
5. Given the string s := "GoπLang", what will len(s) return and why?
hard
A. 7, because len counts characters
B. 8, because emoji counts as two bytes
C. 10, because emoji is 4 bytes plus other characters
D. 6, because emoji is ignored
Solution
Step 1: Analyze string content and encoding
The string "GoπLang" has 7 characters: G, o, π, L, a, n, g. The emoji π is a Unicode character encoded in UTF-8 using 4 bytes.
Step 2: Calculate total byte length
Characters G, o, L, a, n, g are 1 byte each (6 bytes). The emoji is 4 bytes. Total bytes = 6 + 4 = 10 bytes.
Step 3: Verify len(s) output
len(s) returns the number of bytes, so it returns 10, not the number of characters.
Final Answer:
10, because emoji is 4 bytes plus other characters -> Option C
Quick Check:
len counts bytes, emoji uses multiple bytes [OK]
Hint: len() counts bytes, not characters; emojis use multiple bytes [OK]
Common Mistakes:
Assuming len counts characters, not bytes
Ignoring multi-byte encoding of emojis
Thinking emoji counts as one byte
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.