Go String Data Type
Go (Golang) – String Data Type
The string data type in Go is used to store text (sequence of characters).
Go strings are immutable and UTF-8 encoded, which makes them safe and powerful for modern applications.
1️⃣ Declaring Strings
Using var
Type Inference
Empty String
2️⃣ String Literals
🔹 Interpreted String (Double Quotes)
-
Supports escape sequences
🔹 Raw String (Backticks)
-
No escape sequences
-
Multi-line strings supported
3️⃣ String Immutability
Strings cannot be changed after creation.
❌ Invalid:
✔ Correct (create new string):
4️⃣ String Length
Use len() to get number of bytes, not characters.
⚠ For Unicode characters:
5️⃣ Accessing Characters (Bytes & Runes)
Byte Access
Rune (Unicode-safe)
6️⃣ String Concatenation
Using +
Using fmt.Sprintf
7️⃣ Common String Functions (strings package)
8️⃣ String Comparison
9️⃣ Convert String ↔ Number
String to Int
Int to String
🔟 Best Practices
✔ Use raw strings for HTML / SQL
✔ Use strings.Builder for heavy concatenation
✔ Use rune loops for Unicode safety
✔ Avoid modifying strings directly
Summary
-
Strings are immutable
-
UTF-8 encoded
-
Two types: interpreted & raw
-
Rich standard library support
