Golang String Handling
String Representation in Go
In Go, strings are immutable sequences of bytes, typically encoded in UTF-8. Understanding how Go handles strings is crucial for effective text processing.
String Basics
String Declaration and Initialization
package main
import "fmt"
func main() {
// UTF-8 string declaration
str1 := "Hello, 世界" // Unicode string
str2 := `Multi-line
string literal`
fmt.Println(str1)
fmt.Println(str2)
}
String Internals
graph LR
A[Go String] --> B[Byte Slice]
B --> C[UTF-8 Encoding]
C --> D[Rune Representation]
Key String Characteristics
Characteristic |
Description |
Immutability |
Strings cannot be modified after creation |
UTF-8 Encoding |
Default encoding for string data |
Rune Support |
Easy handling of Unicode characters |
String Manipulation Techniques
Iterating Through Strings
func stringIteration() {
text := "Hello, 世界"
// Byte iteration
for i := 0; i < len(text); i++ {
fmt.Printf("Byte %d: %c\n", i, text[i])
}
// Rune iteration
for index, runeValue := range text {
fmt.Printf("Rune at %d: %c\n", index, runeValue)
}
}
String Conversion and Manipulation
func stringConversion() {
// Convert string to rune slice
str := "Hello, 世界"
runes := []rune(str)
// Convert rune slice back to string
newStr := string(runes)
// Length comparisons
fmt.Println("Byte length:", len(str))
fmt.Println("Rune count:", len(runes))
}
Advanced String Handling
Using unicode Package
import (
"fmt"
"unicode"
)
func unicodeHandling() {
text := "Hello, 世界"
// Check character properties
for _, r := range text {
fmt.Printf("Character %c: ", r)
if unicode.Is(unicode.Han, r) {
fmt.Println("Chinese character")
} else {
fmt.Println("Non-Chinese character")
}
}
}
Best Practices
- Use
range
for safe string iteration
- Prefer
rune
for character-level operations
- Be aware of byte vs. character length
- Utilize
unicode
package for advanced processing
Note: Explore more advanced string techniques with LabEx's comprehensive Go programming resources.